Sql 按表分组,然后将另一个表计数添加到第一个表分组计数中
我想按第一个表分组,然后将第二个和第三个表的结果与分组计数相加。。。但是使用虚拟服务代码Sql 按表分组,然后将另一个表计数添加到第一个表分组计数中,sql,Sql,我想按第一个表分组,然后将第二个和第三个表的结果与分组计数相加。。。但是使用虚拟服务代码 select servicecode, Count(*) as count from servicerequest where To_Char(CREATEDATE,'YYYY') = 2018 and status = 50 group by servicecode union select Count(*) as count from CERTREQU
select servicecode, Count(*) as count from servicerequest
where To_Char(CREATEDATE,'YYYY') = 2018
and status = 50
group by servicecode
union
select Count(*) as count from CERTREQUEST
WHERE to_char(REQDATE,'yyyy') = 2018
and service = 1
union
select Count(*) as count from LTR
WHERE to_char(REQDATE,'yyyy') = 2018
and Service = 1
只需在以下两个查询中选择它:
select servicecode, Count(*) as count from servicerequest
where To_Char(CREATEDATE,'YYYY') = 2018
and status = 50
group by servicecode
union
select 1, Count(*) as count from CERTREQUEST
WHERE to_char(REQDATE,'yyyy') = 2018
and service = 1
union
select 2, Count(*) as count from LTR
WHERE to_char(REQDATE,'yyyy') = 2018
and Service = 1
使参数编号相同我使用emty字符串,因为我假设
servicecode
是varchar,根据数据类型将值放在这里
select servicecode, Count(*) as count from servicerequest
where To_Char(CREATEDATE,'YYYY') = 2018
and status = 50
group by servicecode
union all
select '', Count(*) as count from CERTREQUEST
WHERE to_char(REQDATE,'yyyy') = 2018
and service = 1
union all
select '',Count(*) as count from LTR
WHERE to_char(REQDATE,'yyyy') = 2018
and Service = 1
我更喜欢union all只是一个小的点(太小了,我无法编辑)-在第三个查询中,
''计数(*)
之间缺少一个逗号。还值得一提的是,UNION-ALL
保留重复的行,而UNION
将聚合它们。OP可能没有意识到这一点。欢迎访问SO。我建议你阅读这篇文章,因为它为新手提供了非常有用的关于如何写问题的信息。质量问题帮助我们为您提供高质量的答案-我了解您想要实现的目标,但您在使用SQL脚本时遇到的问题是什么?