有没有办法根据两个不同的方式填充列?

时间:2015-11-18 21:43:59

标签: excel oracle pivot

我想在oracle中实现这样的目标。我知道excel中有一个函数,如 = IF(SUMPRODUCT(($ A $ 2:$ A2 = A2)($ B $ 2:$ B2 = B2))> 1,0,1) *可以根据2的不同组合填充列。在oracle中也可以这样做吗?原因是,在excel中,我有大约110K的字符串记录,因此使用此公式填充列的操作非常慢,因为pivot再次重新计算它。

此外,当我们将数据导出到excel时,我们可以使用pivot>这些第3列的总和,用于查看不同的计数,按第1列分组。

confused

1 个答案:

答案 0 :(得分:0)

使用简单的ROW_NUMBER分析函数:

WITH data (class, name) AS (
    SELECT 'Class1', 'James' FROM DUAL UNION ALL
    SELECT 'Class1', 'Sara' FROM DUAL UNION ALL
    SELECT 'Class1', 'Alan' FROM DUAL UNION ALL
    SELECT 'Class1', 'Sara' FROM DUAL UNION ALL
    SELECT 'Class2', 'Cheryl' FROM DUAL UNION ALL
    SELECT 'Class2', 'Cheryl' FROM DUAL UNION ALL
    SELECT 'Class2', 'Cheryl' FROM DUAL UNION ALL
    SELECT 'Class3', 'Alice' FROM DUAL UNION ALL
    SELECT 'Class3', 'Flora' FROM DUAL UNION ALL
    SELECT 'Class3', 'Franklin' FROM DUAL UNION ALL
    SELECT 'Class3', 'Nicol' FROM DUAL UNION ALL
    SELECT 'Class3', 'Franklin' FROM DUAL UNION ALL
    SELECT 'Class4', 'Bess' FROM DUAL UNION ALL
    SELECT 'Class4', 'Lucy' FROM DUAL UNION ALL
    SELECT 'Class4', 'William' FROM DUAL UNION ALL
    SELECT 'Class4', 'Bess' FROM DUAL UNION ALL
    SELECT 'Class4', 'Lucy' FROM DUAL
)
SELECT
    data.*,
    CASE WHEN ROW_NUMBER() OVER (PARTITION BY class, name ORDER BY ROWNUM) = 1 THEN 1 ELSE 0 END helper
FROM
    data
ORDER BY
    ROWNUM