SQL函数PIVOT有2列

时间:2018-04-16 16:13:37

标签: sql oracle oracle-sqldeveloper

我在SQL Developer上有这个查询:

  

SELECT A.order_number,A.TOP_MODEL_LINE_ID,C.CHARGE_NAME,C.CHARGE_VALUE   来自TABLE1 A,TABLE 2B,TABLE 3 C.   其中C.list_line_id = B.list_line_id AND C.line_id = A.TOP_MODEL_LINE_ID和A.order_number =' 4411001286'

提取这4列:

ORDER_NUMBER    LINE_NUMBER     CHARGE_NAME                     CHARGE_VALUE

4411001286      9438572         H Ar                              -639,31
4411001286      9438572         TC Tot                             10867
4411001286      9438572         MN PreV                             6500
4411001286      9438572         LO Offloading                       1200
4411001286      9438572         Costi trasporto                     6500
4411001286      9438572         ML_ICP Machine Line s/n           -24427,69
4411001286      9482805         TC Tot                              23364
4411001286      9482805         H Arr                                0,05
4411001286      9482805         LO Off                               1200
4411001286      9482805         MN Prev Maintenance                  6500
4411001286      9482805         Costi trasporto                      6500
4411001286      9482805         J Margine Ufficio di Rappresentanza  29726
4411001286      9482805         O_ICP Installation                   11954
4411001286      9482805         ML_ICP Machine Line s/n             24427,69
4411001286      9482805         L_ICP 3^ party inst Intercompany        860

我需要查询来提取该结果(从第一个查询中标出):

ORDER_NUMBER  LINE_NUMBER  NAME1    VALUE1    NAME2   VALUE2  NAME3   VALUE3..
4411001286    9438572      H Ar     -639,31   TC TOT  10867   MN Prev  6500...
4411001286    9482805      TC ToT    23364    H Arr   0,05    LO Off   1200...

1 个答案:

答案 0 :(得分:0)

    select order_number, line_number, 
     isnull(max(case when linenum=1 then name else null end),'') name1 ,
     isnull(max(case when linenum=1 then value else null end),'') value1,
      isnull(max(case when linenum=2 then name  else null end),'') name2,
      isnull(max(case when linenum=2 then  value else null end),'') value2,
      isnull(max(case when linenum=3 then name  else null end),'') name3,
      isnull(max(case when linenum=3 then  value else null end),'') value3,
            isnull(max(case when linenum=4 then name  else null end),'') name4,
      isnull(max(case when linenum=4 then  value else null end),'') value4,
            isnull(max(case when linenum=5 then name  else null end),'') name5,
      isnull(max(case when linenum=5 then  value else null end),'') value5,
            isnull(max(case when linenum=6 then name  else null end),'') name6,
      isnull(max(case when linenum=6 then  value else null end),'') value6,
            isnull(max(case when linenum=7 then name  else null end),'') name7,
      isnull(max(case when linenum=7 then  value else null end),'') value7,
             isnull(max(case when linenum=8 then name  else null end),'') name8,
      isnull(max(case when linenum=8 then  value else null end),'') value8,
             isnull(max(case when linenum=9 then name  else null end),'') name9,
      isnull(max(case when linenum=9 then  value else null end),'') value9
          from (select *, row_number() over(partition by order_number, line_number  order by order_number, line_number) linenum
 from (SELECT A.order_number, A.TOP_MODEL_LINE_ID, C.CHARGE_NAME, C.CHARGE_VALUE from TABLE1 A, TABLE2 B ,TABLE3 C where C.list_line_id=B.list_line_id AND C.line_id=A.TOP_MODEL_LINE_ID and A.order_number ='4411001286')b)a
          group by order_number, line_number