根据字段值输出两个不同的列

时间:2017-08-23 21:01:55

标签: sql oracle

我使用Oracle SQL而且我是一个菜鸟。

    table:example
    +--------------+--------------+-------------+
    | id_nr        |  code        | subtype     |
    +-------------------------------------------+
    | 1445         | 40D1343      | DRG         |
    | 1445         | 23423asas    | PROC        |
    | 1234         | 234.34K      | PROC        |
    | 1233         | 2230S        | DRG         |
    | 1233         | 996k         | DRG         |
    | 1233         | 55239        | PROC        |
    +--------------+--------------+-------------+

根据字段子类型(' PROC'或' DRG')中的两个值,代码字段值来自同一行应输出两个不同的列(' PROC' - > ipc_code ,' DRG' - > asc_code )。

table:example
+--------------+-------------+-------------+-----------+
| id_nr        | subtype     | ipc_code    | asc_code  |
+------------------------------------------------------+
| 1445         | DRG         |             | 40D1343   |
| 1445         | PROC        | 23423asas   |           |
| 1234         | PROC        | 234.34K     |           |
| 1233         | DRG         |             | 2230S     |
| 1233         | DRG         |             | 996k      |
| 1233         | PROC        | 55239       |           |
+--------------+-------------+-------------+-----------+

1 个答案:

答案 0 :(得分:1)

您只想要case

select id_nr, subtype,
       (case when subtype = 'PROC' then code end) as ipc_code,
       (case when subtype = 'DRG' then code end) as asc_code
from t;