如何从选择查询中的联接表中获取记录数?

时间:2017-09-29 14:51:49

标签: sql sql-server tsql

我有一个返回地区的查询,我想得到地区联系人的计数,我不知道如何在我的选择语句中执行此操作

select语句是,我已加入联系人表

select 
    dist.DistrictID,
    dist.DistrictName,
    sc.CountyName,
    sp.StateProvince
from [AccountDistrictXREF] adx
    inner join [District] dist on dist.DistrictID = adx.DistrictID
    inner join [StateProvince] sp on sp.StateProvinceID = dist.StateID
    inner join [DistrictStateCountyXREF] dscx on dscx.DistrictID = adx.DistrictID
    left join [StateCounty] sc on sc.StateCountyID = dscx.StateCountyID
    left join [DistrictContactXREF] dcx on dcx.DistrictID = dist.DistrictID
    left join [Contact] c on c.ContactID = dcx.ContactID
where adx.AccountID = 2

我正在尝试

select 
    dist.DistrictID,
    dist.DistrictName,
    sc.CountyName,
    sp.StateProvince,
    count(c.ContactID) as "Contact Count"
from [AccountDistrictXREF] adx
    inner join [District] dist on dist.DistrictID = adx.DistrictID
    inner join [StateProvince] sp on sp.StateProvinceID = dist.StateID
    inner join [DistrictStateCountyXREF] dscx on dscx.DistrictID = adx.DistrictID
    left join [StateCounty] sc on sc.StateCountyID = dscx.StateCountyID
    left join [DistrictContactXREF] dcx on dcx.DistrictID = dist.DistrictID
    left join [Contact] c on c.ContactID = dcx.ContactID
where adx.AccountID = 2

但我收到有关聚合函数和分组依据

的错误

修改

这是第一个查询返回的内容

First Query

我正在寻找的是有一个列中包含该地区的联系人数量,并且该地区可能没有联系人,我也需要考虑到这一点

3 个答案:

答案 0 :(得分:1)

您可能想要加窗COUNT()

select 
    dist.DistrictID,
    dist.DistrictName,
    sc.CountyName,
    sp.StateProvince,
    count(c.ContactID) OVER(PARTITION BY district_id) AS [Contact Count]
from [AccountDistrictXREF] adx
    inner join [District] dist on dist.DistrictID = adx.DistrictID
    inner join [StateProvince] sp on sp.StateProvinceID = dist.StateID
    inner join [DistrictStateCountyXREF] dscx on dscx.DistrictID = adx.DistrictID
    left join [StateCounty] sc on sc.StateCountyID = dscx.StateCountyID
    left join [DistrictContactXREF] dcx on dcx.DistrictID = dist.DistrictID
    left join [Contact] c on c.ContactID = dcx.ContactID
where adx.AccountID = 2;

另一种选择是使用GROUP BY

select 
    dist.DistrictID,
    dist.DistrictName,
    sc.CountyName,
    sp.StateProvince,
    count(c.ContactID) AS [Contact Count]
from [AccountDistrictXREF] adx
    inner join [District] dist on dist.DistrictID = adx.DistrictID
    inner join [StateProvince] sp on sp.StateProvinceID = dist.StateID
    inner join [DistrictStateCountyXREF] dscx on dscx.DistrictID = adx.DistrictID
    left join [StateCounty] sc on sc.StateCountyID = dscx.StateCountyID
    left join [DistrictContactXREF] dcx on dcx.DistrictID = dist.DistrictID
    left join [Contact] c on c.ContactID = dcx.ContactID
where adx.AccountID = 2
GROUP BY dist.DistrictID,
    dist.DistrictName,
    sc.CountyName,
    sp.StateProvince;

第一种方法会为每行提供简单的select ... from .. join加上每组的计数,第二种方法会为您提供聚合值。

答案 1 :(得分:1)

您正在错误地收到群组,因为您需要按

添加群组
select 
    dist.DistrictID,
    dist.DistrictName,
    sc.CountyName,
    sp.StateProvince,
    count(c.ContactID) as "Contact Count"
from [AccountDistrictXREF] adx
    inner join [District] dist on dist.DistrictID = adx.DistrictID
    inner join [StateProvince] sp on sp.StateProvinceID = dist.StateID
    inner join [DistrictStateCountyXREF] dscx on dscx.DistrictID = adx.DistrictID
    left join [StateCounty] sc on sc.StateCountyID = dscx.StateCountyID
    left join [DistrictContactXREF] dcx on dcx.DistrictID = dist.DistrictID
    left join [Contact] c on c.ContactID = dcx.ContactID
where adx.AccountID = 2
GROUP BY dist.DistrictID,
    dist.DistrictName,
    sc.CountyName,
    sp.StateProvince

答案 2 :(得分:1)

所有聚合(总和,计数,平均值,最小值,最大值等)通常需要group by子句。如果没有group by,则假定所有内容都是累计总数,并且结果为一行。

此外,具有group by的查询通常需要将所有NON聚合字段作为group by的一部分。如果您知道给定字段将始终相同,则可以通过应用MAX(区域名称等字段值)来伪造它,而不要将其作为分组的一部分。

select 
      dist.DistrictID,
      dist.DistrictName,
      sc.CountyName,
      sp.StateProvince,
      count(c.ContactID) as "Contact Count"
   from 
      AccountDistrictXREF adx
         join District dist 
            on adx.DistrictID = dist.DistrictID
            join StateProvince sp 
               on dist.StateID = sp.StateProvinceID
            left join DistrictContactXREF dcx 
               on dist.DistrictID = dcx.DistrictID
               left join Contact c 
                  on dcx.ContactID = c.ContactID
         join DistrictStateCountyXREF dscx 
            on adx.DistrictID = dscx.DistrictID
            left join StateCounty sc 
               on dscx.StateCountyID = sc.StateCountyID 
   where 
      adx.AccountID = 2
   group by
      dist.DistrictID,
      dist.DistrictName,
      sc.CountyName,
      sp.StateProvince