在下面的代码中,左连接不显示左边的所有记录!!!
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 :(得分: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