SQL语句中的union怎样将相同数据合并 ,急急急
when month(CreateDate) in (7,8,9) then '3季度' when month(CreateDate) in (10,11,12) then '4季度' end ,
sum(AllOutput) as 削减量,sum(AllAllowOutput) as 排放量,sum(AllPreOutput) as 批复量
from DirtyOutputControl where AliasName='化学需氧量' and Year(CreateDate) = Year(getdate())
group by
Case when month(CreateDate) in (1,2,3) then '1季度' when month(CreateDate) in (4,5,6) then '2季度'
when month(CreateDate) in (7,8,9) then '3季度' when month(CreateDate) in (10,11,12) then '4季度' end
union
select '1季度' as [quarter],0 as AllOutput,0 as AllAllowOutput,0 as AllPreOutput
union
select '2季度' as [quarter],0 as AllOutput,0 as AllAllowOutput,0 as AllPreOutput
union
select '3季度' as [quarter],0 as AllOutput,0 as AllAllowOutput,0 as AllPreOutput
union
select '4季度' as [quarter],0 as AllOutput,0 as AllAllowOutput,0 as AllPreOutput
查询出的数据是这样的子的
1季度 NULL 2.00000 2.00000
1季度 0.00000 0.00000 0.00000
2季度 NULL NULL NULL
2季度 0.00000 0.00000 0.00000
3季度 0.00000 0.00000 0.00000
4季度 0.00000 0.00000 0.00000
我想查询出的效果是这样子的
1季度 0.00000 2.00000 2.00000
2季度 0.00000 0.00000 0.00000
3季度 0.00000 0.00000 0.00000
4季度 0.00000 0.00000 0.00000
相关推荐
SQL语句 UNION 和 UNION ALL 使用
SQL语句 UNION 和 UNION ALL 使用
sql Union和Union All的使用方法
UNION指令的目的是将两个SQL语句的结果合并起来。从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料。
SQL语句之Union和Union All的用法
SQL中Union和Join语句非常类似,但是Union有一个限制条件:两个 SQL 语句所产生的栏位需要是同样的资料种类,最后的结果会看到不同的资料值。 格式: [SQL 语句 1] UNION [SQL 语句 2] 例如: 表store_info 和表internet_sales 使用Union语句: 代码如下:SELECT Date FROM Store_Info UNION SELECT Date FROM Internet_Sales 结果: 或: 代码如下:SELECT Date,Sales FROM Store_Info UNION SELECT Date,Sales
sqlserver Union和SQL Union All使用方法
SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集。 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。 SQL UNION 语法SELECT column_name(s) FROM table_name1UNIONSELECT column_name(s) FROM table_name2 注释:默认地,UNION 操作符选取不同的值,即UNION是去了重的。如果允许重复的值,请使用 UNION ALL。 SQL UNION ALL 语法SELECT
sql 查询结果合并union all用法_数据库技巧
sql语句查询结果合并union all用法_数据库技巧,需要的朋友可以参考下。


