1个回答
展开全部
--测试环境mssql2008
--这就是行列转换
Create TAble T
(
日期 date,
时间 time,
设备 Varchar(10),
数值 int
)
insert into T values('2015-10-15','10:11:12',1,10)
insert into T values('2015-10-15','10:11:12',2,11)
insert into T values('2015-10-15','10:11:12',3,12)
insert into T values('2015-10-15','10:11:12',4,13)
insert into T values('2015-10-15','10:11:12',5,14)
insert into T values('2015-10-15','10:11:12',6,10)
insert into T values('2015-10-15','10:11:12',7,11)
insert into T values('2015-10-15','10:12:12',1,2)
insert into T values('2015-10-15','10:12:12',2,3)
insert into T values('2015-10-15','10:12:12',3,4)
select * from t
pivot
(
sum(数值)
for
设备 in([1],[2],[3],[4],[5],[6],[7])
)p
select 日期,时间,
SUM(Case when 设备=1 then 数值 else 0 end) As [1],
SUM(Case when 设备=2 then 数值 else 0 end) As [2],
SUM(Case when 设备=3 then 数值 else 0 end) As [3],
SUM(Case when 设备=4 then 数值 else 0 end) As [4],
SUM(Case when 设备=5 then 数值 else 0 end) As [5],
SUM(Case when 设备=6 then 数值 else 0 end) As [6],
SUM(Case when 设备=7 then 数值 else 0 end) As [7]
from t
Group by 日期,时间
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |