我有一张叫做设备的桌子。
Unit_ID SensorSerial
1 11
1 22
1 33
2 44
2 55
2 66
3 77
3 88
3 99
我想查询下面的表格:
Unit_ID Sensor1 Sensor2 传感器3
1 11 22 33
2 44 55 66
3 77 88 99
答案 0 :(得分:3)
你可以试试这个
select Unit_ID,
max(case when seqnum = 1 then SensorSerial end) as Sensor1,
max(case when seqnum = 2 then SensorSerial end) as Sensor2,
max(case when seqnum = 3 then SensorSerial end) as Sensor3
from (select equipment.*,
row_number() over (partition by Unit_ID order by SensorSerial) as seqnum
from equipment
) equipment
group by Unit_ID;