多行到单行

时间:2017-08-15 01:59:39

标签: sql-server

如果给定多行数据,我该如何检索结果

Show multiple row data

结果基于已执行的列。条件以tserv_id'DISCHARGE'开头,以'ONPOWER'结束。

条件:

  1. 放电至ONPOWER(1排)
  2. 在最后1次之间执行的任何操作)ONPOWER -------------- ONPOWER(1行)
  3. 在最后2次之间执行的任何操作)ONPOWER -------------- ONPOWER(1行)
  4. 在最后3次之间执行的任何事情)ONPOWER --------------- ONPOWEWR(1行)
  5. Output

    代码:

    -Ofast

1 个答案:

答案 0 :(得分:2)

使用聚合:

select min(Case tserv_id when 'Discharge' then PERFORMED end) as tserv_discharge,
       min(Case tserv_id when 'ONPOWER' then PERFORMED end) as tserv_onpower,
       min(Case tserv_id when 'YARD2TRUCK' then PERFORMED end) as tserv_yard2truck,
       min(Case tserv_id when 'FULL OUT' then PERFORMED end) as tserv_fullout,
       min(Case tserv_id when 'FULL IN' then PERFORMED end) as tserv_fullin
from ODS_REEFER
where EQ_NBR = 'EGSU5014174';