根据列值显示单行数据中的多个行

时间:2015-04-16 07:18:40

标签: sql oracle11g

我的数据如下

ID         DATE     COMPLIANCE ISBREAKREDUCED ISSECONDMEALBREAKREDUCED
1208240    4/12/2015    2              1                1

如何在我的基本查询中使用它来显示列ISBREAKREDUCED&列的不同行。如果值为1,则ISSECONDMEALBREAKREDUCED。 此表连接条件基于id和date字段,这就是它选择单行的原因。如何将此行拆分为输出中的多行?

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找典型的 UNPIVOT 查询。

你可以这样做:

SQL> WITH DATA AS(
  2  SELECT 1208240 ID, 1 ISBREAKREDUCED, 1 ISSECONDMEALBREAKREDUCED FROM dual UNION ALL
  3  SELECT 1208241 ID, 2 ISBREAKREDUCED, 3 ISSECONDMEALBREAKREDUCED FROM dual
  4  )
  5  SELECT *
  6  FROM
  7    (SELECT                                                         *
  8    FROM DATA UNPIVOT (ISSECONDMEALBREAKREDUCED FOR ISBREAKREDUCED IN (ISBREAKREDUCED AS 1, ISSECONDMEALBREAKREDUCED AS 1))
  9    )
 10  WHERE ISBREAKREDUCED         =1
 11  AND ISSECONDMEALBREAKREDUCED =1
 12  /

        ID ISBREAKREDUCED ISSECONDMEALBREAKREDUCED
---------- -------------- ------------------------
   1208240              1                        1
   1208240              1                        1

SQL>