SQL将许多列的表简化为较少的列

时间:2016-03-23 04:56:33

标签: sql oracle

我有一张如下所示的表:

PO_ID | PO_TYPE | PO_STAT | MR_ID | MR_TYPE | MR_STAT | IN_ID | IN_TYPE | IN_STAT
  1   |   A1    |   CO    |   1   |   B1    |   DR    |   1   |   C1    |  CO
  2   |   A1    |   CO    |   2   |   B2    |   CO    |   2   |   C1    |  CO

我想将此表格简化为:

ID   |  TYPE  | STAT
 1   |  A1    | CO
 2   |  A1    | CO
 1   |  B1    | DR
 2   |  B2    | CO
 1   |  C1    | CO
 1   |  C1    | CO

怎么可能?

2 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情......

 select PO_ID as ID,PO_TYPE as TYPE,PO_STAT as STAT from table
    union all
 select MR_ID,MR_TYPE,MR_STAT from table

.... and so on

答案 1 :(得分:0)

您可以使用Union all

来完成此操作
SELECT PO_ID AS ID, PO_TYPE AS TYPE, PO_STAT AS STAT FROM TABLE
UNION ALL 
SELECT MR_ID AS ID, MR_TYPE AS TYPE, MR_STAT AS STAT FROM TABLE
UNION ALL
SELECT IN_ID AS ID, IN_TYPE AS TYPE, IN_STAT AS STAT FROM TABLE