[英]SQL LEFT JOIN on Multiple Tables
给定三个表:
Account
UniqueID
Account Group
VR
UniqueID
AccountID
CR
UniqueID
AccountID
在VR
和CR
找到的AccountID
引用Account
的UniqueID
。
我想以获得结果,其示出了我的Account
的组Account
中的第一列和每数据量Account
组的VR
在第二列和最后的每数据量Account
组中柱3形式CR
。
实现这种连接的正确方法是什么?
我的尝试:
SELECT account_group AS 'Account Group',
COUNT(Account.AccountId) AS 'Anzahl Accounts',
COUNT(VR.ActivityId) AS 'VR',
COUNT(Contact.ContactId) AS 'Contact'
FROM [MSCRM].[dbo].[AccountBase]
LEFT JOIN MSCRM.dbo.visit_report_activityBase
ON Account.AccountId = VR.account_id
LEFT JOIN MSCRM.dbo.ContactBase
ON Account.AccountId = Contact.ParentCustomerId
GROUP BY account_group
ORDER BY account_group ASC;
但是没有COUNT(*)
显示我想要的实际金额。
即使是COUNT
的Account.AccountId
表示无效号码。
我相信您正在寻找的东西与众不同:
SELECT account_group as 'Account Group',
count(distinct Account.AccountId) as 'Anzahl Accounts',
count(distinct VR.ActivityId) as 'VR' count(distinct Contact.ContactId) as 'Contact'
FROM [ MSCRM ] . [ dbo ] . [ AccountBase ] as Account
left join MSCRM.dbo.visit_report_activityBase as VR
on Account.AccountId = VR.account_id
left join MSCRM.dbo.ContactBase as Contact
on Account.AccountId = Contact.ParentCustomerId
group by account_group
order by account_group
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.