2014-09-02 67 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

相关问题