繁体   English   中英

在多个表上的SQL LEFT JOIN

[英]SQL LEFT JOIN on Multiple Tables

给定三个表:

  1. Account
    1. UniqueID
    2. Account Group
  2. VR
    1. UniqueID
    2. AccountID
  3. CR
    1. UniqueID
    2. AccountID

VRCR找到的AccountID引用AccountUniqueID

我想以获得结果,其示出了我的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(*)显示我想要的实际金额。

即使是COUNTAccount.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM