如何在 Informix 12.10 中将行转换为列
How to convert Rows into Columns in Informix 12.10
Localcol
SA_Device A
SA_Device B
SA_Device C
2021-05-04 21:27:40
12
13
14
2021-05-04 21:28:00
16
17
18
Localcol
AA_Device A
AA_Device B
2021-05-04 21:27:40
34
43
2021-05-04 21:28:00
45
46
我需要将这 2 个表合并为一个表并按以下格式显示。
Localcol
Device
SA
AA
2021-05-04 21:27:40
Device A
12
34
2021-05-04 21:27:40
Device B
13
43
2021-05-04 21:27:40
Device C
14
NA
2021-05-04 21:28:00
Device A
16
45
2021-05-04 21:28:00
Device B
17
46
2021-05-04 21:28:00
Device C
18
NA
请帮我在 Informix 12.10 中解决这个问题。
嗯。 . .我认为你应该取消每个 table 的旋转,然后加入它们:
select coalesce(t2.localcol, t1.localcol) as localcol,
coalesce(t2.device, t1.device) as device,
t1.SA, t2.AA
from (select Localcol, 'A' as device, AA_Device_A as AA
from table2 t2
union all
select Localcol, 'B' as device, AA_Device_B
from table2 t2
) t2 full join
(select Localcol, 'A' as device, SA_Device_A as SA
from table2 t1
union all
select Localcol, 'B' as device, SA_Device_B as SA
from table2 t1
union all
select Localcol, 'C' as device, SA_Device_B as SA
from table2 t1
) t1
on t1.localcol = t2.localcol and t1.device = t2.device
Localcol | SA_Device A | SA_Device B | SA_Device C |
---|---|---|---|
2021-05-04 21:27:40 | 12 | 13 | 14 |
2021-05-04 21:28:00 | 16 | 17 | 18 |
Localcol | AA_Device A | AA_Device B |
---|---|---|
2021-05-04 21:27:40 | 34 | 43 |
2021-05-04 21:28:00 | 45 | 46 |
我需要将这 2 个表合并为一个表并按以下格式显示。
Localcol | Device | SA | AA |
---|---|---|---|
2021-05-04 21:27:40 | Device A | 12 | 34 |
2021-05-04 21:27:40 | Device B | 13 | 43 |
2021-05-04 21:27:40 | Device C | 14 | NA |
2021-05-04 21:28:00 | Device A | 16 | 45 |
2021-05-04 21:28:00 | Device B | 17 | 46 |
2021-05-04 21:28:00 | Device C | 18 | NA |
请帮我在 Informix 12.10 中解决这个问题。
嗯。 . .我认为你应该取消每个 table 的旋转,然后加入它们:
select coalesce(t2.localcol, t1.localcol) as localcol,
coalesce(t2.device, t1.device) as device,
t1.SA, t2.AA
from (select Localcol, 'A' as device, AA_Device_A as AA
from table2 t2
union all
select Localcol, 'B' as device, AA_Device_B
from table2 t2
) t2 full join
(select Localcol, 'A' as device, SA_Device_A as SA
from table2 t1
union all
select Localcol, 'B' as device, SA_Device_B as SA
from table2 t1
union all
select Localcol, 'C' as device, SA_Device_B as SA
from table2 t1
) t1
on t1.localcol = t2.localcol and t1.device = t2.device