数据库查询问题,各位大神看一下。

z1_23456 2018-11-10 03:22:33
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

怎么让下面为空的数据求和显示为一条。
...全文
33 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
z1_23456 2018-11-12
  • 打赏
  • 举报
回复
嗯,谢谢解答。解决问题了。
卖水果的net 2018-11-11
  • 打赏
  • 举报
回复
引用 2 楼 z1_23456 的回复:
请问我在标记地方添加tblTask查询条件对结果有没有影响,谢谢你解答。
因为,你的需求是把最后几行数汇在一起,所以,要用 group by 子句,而 where 并不能替代 group by .
吉普赛的歌 2018-11-11
  • 打赏
  • 举报
回复
1. 还要连接其它表, 这些业务逻辑只有你自己明白, 最好是你自己弄清楚, 不要偷懒。 2. 分页, 在 #1 版主 基础上稍改下就好:
;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 
--(@pageIndex-1)*@pageSize+1 and @pageIndex*@pageSize

z1_23456 2018-11-11
  • 打赏
  • 举报
回复
1.我想问的是,只是添加一些表单筛选条件在那个地方,并不改变其它的,筛选之后连接其它的表,对结果没有影响吧?
2.我想进行分页,这样select * from ({0}) t where rowNumber between {1} and {2},{0}里面怎么引用?
不是很懂,求大神指导。
z1_23456 2018-11-10
  • 打赏
  • 举报
回复
z1_23456 2018-11-10
  • 打赏
  • 举报
回复
[img=https://img-bbs.csdn.net/upload/201811/10/1541845097_557499.png][/img
请问我在标记地方添加tblTask查询条件对结果有没有影响,谢谢你解答。
卖水果的net 2018-11-10
  • 打赏
  • 举报
回复


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
-- 上面是你原来的
)
select DepartmentName,
       SectionOffice,
       sum(ShareCount) ShareCount
  from m
  group by 
       DepartmentName,
       SectionOffice

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧