根据Oracle 11g sql中的2列将行转置为列

时间:2016-06-07 08:40:42

标签: oracle oracle11g

我有一个表(EMP_Comm_Calc),如下所示:

Min-Wage    Max-Wage    Comm%   Work_Type_ID    Working_Days    Max_Allowed Min_Allowed
400 2000    30  2426    20  2000    1888
450 2000    30  2426    30  2000    1888
1680    2000    54  2426    20  2000    1888
1680    2000    54  2426    30  2000    1888
1900    2000    65  2426    20  2000    1888
1950    2000    65  2426    30  2000    1888
450 2500    30  2427    20  2500    1999
450 2500    30  2427    30  2500    1999
2100    2500    54  2427    20  2500    1999
2100    2500    54  2427    30  2500    1999
2380    2500    65  2427    20  2500    1999
2380    2500    65  2427    30  2500    1999

现在我想根据每个Work_type_id的Comm%和Working_days转置行,预期输出为:

Work_Type_ID    Min2065 Max2065 Min2030 Max2030 Min2054 Max2054 Min3065 Max3065 Min3030 Max3030 Min3054 Max3054 Max_allowed Min_Allowed
2426    1900    2000    400 2000    1680    2000    1950    2000    450 2000    1680    2000    2000    1888
2427    2380    2500    450 2500    2100    2500    2380    2500    450 2500    2100    2500    2500    1999

1 个答案:

答案 0 :(得分:0)

Oracle安装程序

CREATE TABLE table_name ( Min_Wage, Max_Wage, "Comm%", Work_Type_ID, Working_Days, Max_Allowed, Min_Allowed ) AS
SELECT 400, 2000, 30, 2426, 20, 2000, 1888 FROM DUAL UNION ALL
SELECT 450, 2000, 30, 2426, 30, 2000, 1888 FROM DUAL UNION ALL
SELECT 1680, 2000, 54, 2426, 20, 2000, 1888 FROM DUAL UNION ALL
SELECT 1680, 2000, 54, 2426, 30, 2000, 1888 FROM DUAL UNION ALL
SELECT 1900, 2000, 65, 2426, 20, 2000, 1888 FROM DUAL UNION ALL
SELECT 1950, 2000, 65, 2426, 30, 2000, 1888 FROM DUAL UNION ALL
SELECT 450, 2500, 30, 2427, 20, 2500, 1999 FROM DUAL UNION ALL
SELECT 450, 2500, 30, 2427, 30, 2500, 1999 FROM DUAL UNION ALL
SELECT 2100, 2500, 54, 2427, 20, 2500, 1999 FROM DUAL UNION ALL
SELECT 2100, 2500, 54, 2427, 30, 2500, 1999 FROM DUAL UNION ALL
SELECT 2380, 2500, 65, 2427, 20, 2500, 1999 FROM DUAL UNION ALL
SELECT 2380, 2500, 65, 2427, 30, 2500, 1999 FROM DUAL;

<强>查询

SELECT *
FROM   (
  SELECT Work_Type_ID,
         Working_Days||"Comm%" AS pivotvalue,
         Min_Wage,
         Max_Wage,
         Max_Allowed,
         Min_Allowed
  FROM   table_name
)
PIVOT (
  MIN( Min_wage ) AS Min,
  MAX( Max_Wage ) AS Max
  FOR pivotvalue IN ( 2065, 2030, 2054, 3065, 3030, 3054 )
);

<强>输出

WORK_TYPE_ID MAX_ALLOWED MIN_ALLOWED   2065_MIN   2065_MAX   2030_MIN   2030_MAX   2054_MIN   2054_MAX   3065_MIN   3065_MAX   3030_MIN   3030_MAX   3054_MIN   3054_MAX
------------ ----------- ----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
        2426        2000        1888       1900       2000        400       2000       1680       2000       1950       2000        450       2000       1680       2000 
        2427        2500        1999       2380       2500        450       2500       2100       2500       2380       2500        450       2500       2100       2500