我有一些类似的数据:
+---------+---------+---------+-------+
| MAXIMUM | MINIMUM | SENSORS | TIME |
+---------+---------+---------+-------+
| 10 | 12 | 14 | 13:12 |
| 80 | 70 | 100 | 14:54 |
+---------+---------+---------+-------+
但我需要这样的事情:
+---------+-------+
| X | Y |
+---------+-------+
| MAXIMUM | 10 |
| MINIMUM | 12 |
| SENSORS | 14 |
| TIME | 13:12 |
| MAXIMUM | 80 |
| MINIMUM | 70 |
| SENSORS | 100 |
| TIME | 14:54 |
+---------+-------+
如何获取此类数据是否有可能获取数据?
答案 0 :(得分:3)
只是另一种选择
示例强>
Select B.*
From YourTable
Cross Apply (values ('MAXIMUM',convert(nvarchar(50),MAXIMUM))
,('MINIMUM',convert(nvarchar(50),MINIMUM))
,('SENSORS',SENSORS)
,('TIME' ,convert(nvarchar(50),[TIME],108))
) B(x,y)
<强>返回强>
x y
MAXIMUM 10
MINIMUM 12
SENSORS 14
TIME 13:12:00
MAXIMUM 80
MINIMUM 70
SENSORS 100
TIME 14:54:00
答案 1 :(得分:2)
您可以使用UNPIVOT
:
declare @tmp table (MAXIMUM nvarchar(10), MINIMUM nvarchar(10), SENSORS nvarchar(10), [TIME] nvarchar(10))
insert into @tmp select 10,12,14 ,'13:12'
insert into @tmp select 80,70,100,'14:54'
select u.x,u.y
from @tmp s
unpivot
(
[y]
for [x] in ([MAXIMUM],[MINIMUM],[SENSORS],[TIME])
) u;
结果: