将每列的1列数据转换为多列

时间:2016-06-29 07:46:21

标签: oracle oracle11g

我有两个用存储过程创建的表。 Visualization first and second table

第一个表是根据用户选择的ID创建的。

当用户选择两个过滤器时,将创建第二个表。然后将这些过滤器添加到绑定表中。

结果表应该使用第一个表来获取所有数据,第二个表用于匹配每个用户的正确区域和功能。

结果应该是这样的: Image result table

是否可以根据绑定表获取一列中的所有区域以及另一列中该区域中的所有匹配函数?

我正在使用Oracle 11g。

1 个答案:

答案 0 :(得分:0)

下面的sql返回了与[图像结果表]相同的结果。

尝试在Oracle下面的sql下运行。

这就是你想要的吗?

WITH
    COUNTS AS (
        SELECT 120 AS id, 1 AS persionId, 'Region' AS tellingName, 'RegionX' AS tellingValue FROM DUAL
        UNION ALL
        SELECT 120 AS id, 2 AS persionId, 'Region' AS tellingName, 'RegionX' AS tellingValue FROM DUAL
        UNION ALL
        SELECT 120 AS id, 3 AS persionId, 'Region' AS tellingName, 'RegionY' AS tellingValue FROM DUAL
        UNION ALL
        SELECT 120 AS id, 1 AS persionId, 'Function' AS tellingName, 'FunctionX' AS tellingValue FROM DUAL
        UNION ALL
        SELECT 120 AS id, 2 AS persionId, 'Function' AS tellingName, 'FunctionY' AS tellingValue FROM DUAL
        UNION ALL
        SELECT 120 AS id, 3 AS persionId, 'Function' AS tellingName, 'FunctionX' AS tellingValue FROM DUAL
    ),
    BINDING AS (
        SELECT 1 AS seqid, 120 AS id, 'Region' AS "Column", 'Function' AS "Row" FROM DUAL
    ),
    COLUMN_INFO AS (
        SELECT 
            T2.seqid,
            T1.tellingValue AS "Column"
        FROM
            COUNTS T1,
            BINDING T2
        WHERE
            T1.id = T2.id AND
            T1.tellingName = T2."Column"
        GROUP BY T2.seqid, T1.tellingValue
    ),
    ROW_INFO AS (
        SELECT 
            T2.seqid,
            T1.tellingValue AS "Row"
        FROM
            COUNTS T1,
            BINDING T2
        WHERE
            T1.id = T2.id AND
            T1.tellingName = T2."Row"
        GROUP BY T2.seqid, T1.tellingValue
    )
SELECT
    T1.seqid,
    T1."Column",
    T2."Row"
FROM
    COLUMN_INFO T1,
    ROW_INFO T2
WHERE
    T1.seqid = T2.seqid