注册 登录
编程论坛 SQL Server论坛

数据求和显示,求大神指导。

zcdjt 发布于 2018-11-10 15:16, 2046 次点击
select DepartmentName,SectionOffice,
(select count(*) ShareCount from tblTaskFlowRecord where OperationType in (2,3)  and CreateWechatOpenId=c.OpenId group by CreateWechatOpenId) ShareCount--分享量
from (select distinct CreateWechatOpenId from tblTaskFlowRecord) a
join SysUser c on a.CreateWechatOpenId=c.OpenId
left join tblDepartmentRelation d on c.Mobile=d.Phone
只有本站会员才能查看附件,请 登录

怎么把为空的数据统计成一条显示?
1 回复
#2
zcdjt2018-11-12 11:08
;with m as (
-- 下面是你原来的
select DepartmentName,
       SectionOffice,
       (select count(*) ShareCount
          from tblTaskFlowRecord
         where OperationType in (2, 3)
           and CreateWechatOpenId = c.OpenId
         group by CreateWechatOpenId) ShareCount --分享量
  from (select distinct CreateWechatOpenId from tblTaskFlowRecord) a
  join SysUser c
    on a.CreateWechatOpenId = c.OpenId
  left join tblDepartmentRelation d
    on c.Mobile = d.Phone
-- 上面是你原来的
)
,cte AS (
   select
       ROW_NUMBER() OVER (ORDER BY DepartmentName) AS rid,
        DepartmentName,
       SectionOffice,
       sum(ShareCount) ShareCount
  from m
  group by
       DepartmentName,
       SectionOffice
)
SELECT * FROM cte where rowNumber BETWEEN 1 AND 20
1