这样?select sum(数据) 数据 , 单位 from ( select 数据1 数据, 单位1 单位 from tb union all select 数据2 数据, 单位2 单位 from tb ) t group by 单位
select sum(数据) as 数据,单位 from ( select 数据1 as 数据,单位1 as 单位 from tb union all select 数据2,单位2 from tb) group by 单位
create table tb(数据1 int, 数据2 int, 单位1 varchar(10) , 单位2 varchar(10)) insert into tb values(1 , 2 , '起' ,'小时') insert into tb values(2 , 3 , '小时' ,'人') goselect sum(数据) 数据 , 单位 from ( select 数据1 数据, 单位1 单位 from tb union all select 数据2 数据, 单位2 单位 from tb ) t group by 单位drop table tb /*数据 单位 ----------- ---------- 1 起 3 人 4 小时(所影响的行数为 3 行) */
---测试数据--- if object_id('[tb]') is not null drop table [tb] go create table [tb]([数据1] int,[数据2] int,[单位1] varchar(4),[单位2] varchar(4)) insert [tb] select 1,2,'起','小时' union all select 2,3,'小时','人'
---查询--- select sum(数据) as 数据,单位 from ( select 数据1 as 数据,单位1 as 单位 from tb union all select 数据2,单位2 from tb) t group by 单位---结果--- 数据 单位 ----------- ---- 1 起 3 人 4 小时(所影响的行数为 3 行)
(
select 数据1 数据, 单位1 单位 from tb
union all
select 数据2 数据, 单位2 单位 from tb
) t
group by 单位
from (
select 数据1 as 数据,单位1 as 单位 from tb
union all
select 数据2,单位2 from tb)
group by 单位
insert into tb values(1 , 2 , '起' ,'小时')
insert into tb values(2 , 3 , '小时' ,'人')
goselect sum(数据) 数据 , 单位 from
(
select 数据1 数据, 单位1 单位 from tb
union all
select 数据2 数据, 单位2 单位 from tb
) t
group by 单位drop table tb /*数据 单位
----------- ----------
1 起
3 人
4 小时(所影响的行数为 3 行)
*/
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([数据1] int,[数据2] int,[单位1] varchar(4),[单位2] varchar(4))
insert [tb]
select 1,2,'起','小时' union all
select 2,3,'小时','人'
---查询---
select sum(数据) as 数据,单位
from (
select 数据1 as 数据,单位1 as 单位 from tb
union all
select 数据2,单位2 from tb) t
group by 单位---结果---
数据 单位
----------- ----
1 起
3 人
4 小时(所影响的行数为 3 行)