我如何查询中的计数总和?附加的SQL Server查询

时间:2019-01-09 08:42:28

标签: sql-server group-by count sum

我想使用查询中已经在使用的按计数总和,

select R.region_id as Id,R.region_name as Name, count(IR.Id) as AgencyreportCount,count(AR.Id) as IndividualreportCount 
     from region R 
left join governorate G
           on r.region_id=g.region_id
left join IndividualReports IR
            on g.governorate_id=IR.governorate_id
left join AgencyReports AR
            on g.governorate_id=AR.governorate_id
left join AgencyUsers AU
            on IR.AgencyUserId=AU.Id
left join Agencies A
            on AU.AgencyId=A.Id where A.Id=1
Group by R.region_id,R.region_name

我想在查询sum(count(IR.Id)+count(AR.Id)) as Total

中使用这些计数求和

2 个答案:

答案 0 :(得分:3)

我相信您不需要SUM运算符,只需将两个COUNT加在一起。

count(IR.Id) + count(AR.Id) as Total

除非您想要区域的总数独立,否则将使用窗口SUM

SUM(count(IR.Id) + count(AR.Id)) OVER () as Total

答案 1 :(得分:1)

我会这样说:

const urlsToCache = [
  './image/fallback.png',
  './offline/'
];

如果您想要总和,只需取出除总和以外的所有内容,然后用SUM()换行即可。