2014-09-02 4 views
0

다음 코드에서 왼쪽의 모든 레코드가 표시되지 않습니다. 별개의 기록은 열left join은 모든 레코드를 표시하지 않습니다.

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 

당신의 위의 질의에 대한 보답 레코드의 수는 당신이 할 수있는 왼쪽 테이블에서 모든 레코드를 얻으려면 ... 동일합니다하여 그룹에 대한 존재 얼마나 많은

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 개의 레코드 만 표시합니다 !! –

+0

에 의해 요약 및 그룹화 중입니다. 기록이 적은 것은 정상입니다. –

+0

@GiannisParaskevopoulos가 합계와 그룹으로이 열을 모두 가지고있는 솔루션을 알려주십시오. –

답변

0

확인 아래 2 가지 방법 중 하나를 사용하지만 두 번째 것을 선호합니다 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

왼쪽 + 위 열의 모든 레코드를 갖기 위해 어떻게해야합니까 ??? –

+0

편집 된 코드를 확인하십시오 ... –

+0

은 매력처럼 작동하지만 알려주십시오. 귀하의 코드가 성능면에서 나 그보다 나은 이유는 무엇입니까? –

관련 문제