基于使用ORACLE / SQL的单个表中的两个条件创建两个单独的列

时间:2016-04-03 04:43:56

标签: sql oracle select where

我有一张像

这样的表格
A1 A2 A3 A4 A5 A6 
1  1   1  1  1  1
2  2   2  2  2  2
3  3   3  3  3  3

我希望完成以下任务:

SELECT A2 AS B2 WHERE A1 = 1
SELECT A2 AS B3 WHERE A1 = 2

我该怎么做?

2 个答案:

答案 0 :(得分:0)

我认为你可以使用这样的查询:

SELECT 
    CASE WHEN A1 = 1 THEN A2 END As B2,
    CASE WHEN A1 = 2 THEN A2 END As B3,
FROM
    yourTable;

请注意,B2A2相同,如果A1 = 1其他为null,依此类推。

答案 1 :(得分:0)

SELECT 
    CASE WHEN A1 = 1 THEN A2 END As B2,
    CASE WHEN A1 = 2 THEN A2 END As B3
FROM TABLE_NAME
WHERE  A1 = 1 OR A1 = 2