2014-09-02 68 views
0

在下面的代碼左加入不顯示所有記錄從左!左加入並不顯示所有記錄

select *,CASE WHEN (ResDEBIT> ResCREDIT) THEN (ResDEBIT) when (ResCREDIT> ResDEBIT)then (ResCREDIT) else 0 END AS Mande,CASE WHEN (ResDEBIT> ResCREDIT) THEN ('debit') when (ResCREDIT> ResDEBIT)then ('credit') ELSE ('ziro') END AS Status from(SELECT  Sales.CustomerInfo.CustomerInfoID,FullTitle=(cast(Sales.CustomerInfo.AccountFK as nvarchar)+' - '+Sales.CustomerInfo.FullName), Sales.CustomerInfo.TopicFK, Sales.CustomerInfo.AccountFK,Sales.CustomerInfo.CompanyRegNo,Sales.CustomerInfo.PersonTypeFK,Sales.CustomerInfo.BankAccountDetail,Sales.CustomerInfo.BankAccountNo, Sales.CustomerInfo.AccountNo, Sales.CustomerInfo.FullName, 
         Sales.CustomerInfo.Birthdate, Sales.CustomerInfo.TitleFK, Sales.CustomerInfo.RegistrationDate, Sales.CustomerInfo.CustomerPhotoFK, Sales.CustomerInfo.SocialNo, 
         Sales.CustomerInfo.WebPage, Sales.CustomerInfo.JobFK, Sales.CustomerInfo.MaxDebitLimit, Sales.CustomerInfo.MaxChequeCredit, 
         Sales.CustomerInfo.PreferedPaymentMethodFK, Sales.CustomerInfo.FirstBalanceKind, Sales.CustomerInfo.FirstBalance, Sales.CustomerInfo.Debit, 
         Sales.CustomerInfo.Credit, Sales.CustomerInfo.Note, Sales.CustomerInfo.FinancialPeriodFK, Sales.CustomerInfo.CompanyInfoFK, 
         isnull(SUM(Accounting.DocumentDetail.Debit),0) AS Debit1, isnull(SUM(Accounting.DocumentDetail.Credit),0) AS Credit1, (CASE WHEN (isnull(SUM(Accounting.DocumentDetail.Credit),0) 
         - isnull(SUM(Accounting.DocumentDetail.Debit),0)) < 0 THEN (isnull(SUM(Accounting.DocumentDetail.Debit),0) - isnull(SUM(Accounting.DocumentDetail.Credit),0)) ELSE 0 END) AS ResDEBIT, 
         (CASE WHEN (isnull(SUM(Accounting.DocumentDetail.Credit),0) - isnull(SUM(Accounting.DocumentDetail.Debit),0)) > 0 THEN (isnull(SUM(Accounting.DocumentDetail.Credit),0) 
         - isnull(SUM(Accounting.DocumentDetail.Debit),0)) ELSE 0 END) AS ResCREDIT,Sales.CustomerInfo.BlackListed, Sales.CustomerInfo.IsActive 
FROM   Sales.CustomerInfo left JOIN 
         Accounting.DocumentDetail ON Sales.CustomerInfo.AccountFK = Accounting.DocumentDetail.TopicFK 
GROUP BY Sales.CustomerInfo.CustomerInfoID, Sales.CustomerInfo.TopicFK, Sales.CustomerInfo.AccountFK, Sales.CustomerInfo.AccountNo, 
         Sales.CustomerInfo.FullName, Sales.CustomerInfo.Birthdate, Sales.CustomerInfo.TitleFK,Sales.CustomerInfo.CompanyRegNo,Sales.CustomerInfo.PersonTypeFK,Sales.CustomerInfo.BankAccountDetail,Sales.CustomerInfo.BankAccountNo, Sales.CustomerInfo.RegistrationDate, 
         Sales.CustomerInfo.CustomerPhotoFK, Sales.CustomerInfo.SocialNo, Sales.CustomerInfo.WebPage, Sales.CustomerInfo.JobFK, Sales.CustomerInfo.MaxDebitLimit, 
         Sales.CustomerInfo.MaxChequeCredit, Sales.CustomerInfo.PreferedPaymentMethodFK, Sales.CustomerInfo.FirstBalanceKind, Sales.CustomerInfo.FirstBalance, 
         Sales.CustomerInfo.Debit, Sales.CustomerInfo.Credit, Sales.CustomerInfo.Note, Sales.CustomerInfo.FinancialPeriodFK, Sales.CustomerInfo.CompanyInfoFK, 
         Sales.CustomerInfo.BlackListed, Sales.CustomerInfo.IsActive) CustomerInfo 
+0

左表有100條記錄,但只顯示9條記錄! – 2014-09-02 05:56:55

+0

你正在總結和分組。減少記錄是正常的。 – 2014-09-02 06:12:22

+0

@GiannisParaskevopoulos讓我知道什麼是解決方案有所有這些列的總和和 – 2014-09-02 06:21:10

回答

0

檢查不同的記錄被列

Select distinct 
     Sales.CustomerInfo.CustomerInfoID 
     ,Sales.CustomerInfo.TopicFK 
     ,Sales.CustomerInfo.AccountFK 
     ,Sales.CustomerInfo.AccountNo 
     ,Sales.CustomerInfo.FullName 
     ,Sales.CustomerInfo.Birthdate 
     ,Sales.CustomerInfo.TitleFK 
     ,Sales.CustomerInfo.CompanyRegNo 
     ,Sales.CustomerInfo.PersonTypeFK 
     ,Sales.CustomerInfo.BankAccountDetail 
     ,Sales.CustomerInfo.BankAccountNo 
     ,Sales.CustomerInfo.RegistrationDate 
     ,Sales.CustomerInfo.CustomerPhotoFK 
     ,Sales.CustomerInfo.SocialNo 
     ,Sales.CustomerInfo.WebPage 
     ,Sales.CustomerInfo.JobFK 
     ,Sales.CustomerInfo.MaxDebitLimit 
     ,Sales.CustomerInfo.MaxChequeCredit 
     ,Sales.CustomerInfo.PreferedPaymentMethodFK 
     ,Sales.CustomerInfo.FirstBalanceKind 
     ,Sales.CustomerInfo.FirstBalance 
     ,Sales.CustomerInfo.Debit 
     ,Sales.CustomerInfo.Credit 
     ,Sales.CustomerInfo.Note 
     ,Sales.CustomerInfo.FinancialPeriodFK 
     ,Sales.CustomerInfo.CompanyInfoFK 
     ,Sales.CustomerInfo.BlackListed 
     ,Sales.CustomerInfo.IsActive 
from Sales.CustomerInfo 

的回報記錄你上面的查詢數量將是一樣的...若要從您的左表中,你可以得到所有的記錄多少存在的組使用以下兩種方法中的任何一種,但我更喜歡第二種方法 1)使用子查詢 2)首先在單獨的查詢中進行聚合,然後再與左側的表進行連接...查詢應該有些相似到以下代碼:

;WITH CTE (AccountFK, Debit1 ,Credit1 ,ResDEBIT ,ResCREDIT ) 
AS (
SELECT 
    Sales.CustomerInfo.AccountFK 
    ,isnull(SUM(Accounting.DocumentDetail.Debit), 0) AS Debit1 
    ,isnull(SUM(Accounting.DocumentDetail.Credit), 0) AS Credit1 
    ,(
     CASE 
      WHEN (isnull(SUM(Accounting.DocumentDetail.Credit), 0) - isnull(SUM(Accounting.DocumentDetail.Debit), 0)) < 0 
       THEN (isnull(SUM(Accounting.DocumentDetail.Debit), 0) - isnull(SUM(Accounting.DocumentDetail.Credit), 0)) 
      ELSE 0 
      END 
     ) AS ResDEBIT 
    ,(
     CASE 
      WHEN (isnull(SUM(Accounting.DocumentDetail.Credit), 0) - isnull(SUM(Accounting.DocumentDetail.Debit), 0)) > 0 
       THEN (isnull(SUM(Accounting.DocumentDetail.Credit), 0) - isnull(SUM(Accounting.DocumentDetail.Debit), 0)) 
      ELSE 0 
      END 
     ) AS ResCREDIT 
FROM Sales.CustomerInfo 
LEFT JOIN Accounting.DocumentDetail ON Sales.CustomerInfo.AccountFK = Accounting.DocumentDetail.TopicFK 
GROUP BY Sales.CustomerInfo.CustomerInfoID 
    ,Sales.CustomerInfo.TopicFK 
    ,Sales.CustomerInfo.AccountFK 
    ,Sales.CustomerInfo.AccountNo 
    ,Sales.CustomerInfo.FullName 
    ,Sales.CustomerInfo.Birthdate 
    ,Sales.CustomerInfo.TitleFK 
    ,Sales.CustomerInfo.CompanyRegNo 
    ,Sales.CustomerInfo.PersonTypeFK 
    ,Sales.CustomerInfo.BankAccountDetail 
    ,Sales.CustomerInfo.BankAccountNo 
    ,Sales.CustomerInfo.RegistrationDate 
    ,Sales.CustomerInfo.CustomerPhotoFK 
    ,Sales.CustomerInfo.SocialNo 
    ,Sales.CustomerInfo.WebPage 
    ,Sales.CustomerInfo.JobFK 
    ,Sales.CustomerInfo.MaxDebitLimit 
    ,Sales.CustomerInfo.MaxChequeCredit 
    ,Sales.CustomerInfo.PreferedPaymentMethodFK 
    ,Sales.CustomerInfo.FirstBalanceKind 
    ,Sales.CustomerInfo.FirstBalance 
    ,Sales.CustomerInfo.Debit 
    ,Sales.CustomerInfo.Credit 
    ,Sales.CustomerInfo.Note 
    ,Sales.CustomerInfo.FinancialPeriodFK 
    ,Sales.CustomerInfo.CompanyInfoFK 
    ,Sales.CustomerInfo.BlackListed 
    ,Sales.CustomerInfo.IsActive 
) 

SELECT Sales.CustomerInfo.CustomerInfoID 
    ,FullTitle = (cast(Sales.CustomerInfo.AccountFK AS NVARCHAR) + ' - ' + Sales.CustomerInfo.FullName) 
    ,Sales.CustomerInfo.TopicFK 
    ,Sales.CustomerInfo.AccountFK 
    ,Sales.CustomerInfo.CompanyRegNo 
    ,Sales.CustomerInfo.PersonTypeFK 
    ,Sales.CustomerInfo.BankAccountDetail 
    ,Sales.CustomerInfo.BankAccountNo 
    ,Sales.CustomerInfo.AccountNo 
    ,Sales.CustomerInfo.FullName 
    ,Sales.CustomerInfo.Birthdate 
    ,Sales.CustomerInfo.TitleFK 
    ,Sales.CustomerInfo.RegistrationDate 
    ,Sales.CustomerInfo.CustomerPhotoFK 
    ,Sales.CustomerInfo.SocialNo 
    ,Sales.CustomerInfo.WebPage 
    ,Sales.CustomerInfo.JobFK 
    ,Sales.CustomerInfo.MaxDebitLimit 
    ,Sales.CustomerInfo.MaxChequeCredit 
    ,Sales.CustomerInfo.PreferedPaymentMethodFK 
    ,Sales.CustomerInfo.FirstBalanceKind 
    ,Sales.CustomerInfo.FirstBalance 
    ,Sales.CustomerInfo.Debit 
    ,Sales.CustomerInfo.Credit 
    ,Sales.CustomerInfo.Note 
    ,Sales.CustomerInfo.FinancialPeriodFK 
    ,Sales.CustomerInfo.CompanyInfoFK 
    ,cte.Debit1 
    ,cte.Credit1 
    ,cte.ResDEBIT 
    ,cte.ResCREDIT 
    ,Sales.CustomerInfo.BlackListed 
    ,Sales.CustomerInfo.IsActive 
    ,CASE 
     WHEN (ResDEBIT > ResCREDIT) 
      THEN (ResDEBIT) 
     WHEN (ResCREDIT > ResDEBIT) 
      THEN (ResCREDIT) 
     ELSE 0 
     END AS Mande 
    ,CASE 
     WHEN (ResDEBIT > ResCREDIT) 
      THEN ('debit') 
     WHEN (ResCREDIT > ResDEBIT) 
      THEN ('credit') 
     ELSE ('ziro') 
     END AS STATUS 
FROM Sales.CustomerInfo 
LEFT JOIN cte ON Sales.CustomerInfo.AccountFK = cte.AccountFK 
+0

我應該怎麼做纔能有所有記錄從左+ +以上列??? – 2014-09-02 06:16:52

+0

只是檢查編輯的代碼... – 2014-09-02 06:41:08

+0

工作就像一個魅力,但請讓我知道如果你的代碼比我的表現更好,爲什麼? – 2014-09-02 07:27:47

相關問題