oracle 在给定的条件下将行和列相加

tsm1rwdh  于 2022-11-22  发布在  Oracle
关注(0)|答案(2)|浏览(175)

我 有 这样 的 疑问 :

select pivot_table.*
from   (
  Select STATUS,USER_TYPE
  FROM   TRANSACTIONS tr 
         join TRANSACTION_STATUS_CODES sc on sc.id = tr.user_type
         join TRANSACTION_USER_TYPES ut on ut.id=tr.user_type 
  WHERE  Tr.User_Type between 1 and 5
  And    tr.status!=1
  AND    Tr.Update_Date BETWEEN TO_DATE('2022-01-01 00:00:00', 'yyyy-mm-dd HH24:MI:SS')
                        AND     TO_DATE('2022-11-13 23:59:59', 'yyyy-mm-dd HH24:MI:SS')
) t
pivot(
  count(user_type)
  FOR user_type IN (1,2,3,5) 
)  pivot_table;

中 的 每 一 个
其 给出 :
| 状态|一 个|2 个|三 个|五 个|
| - -| - -| - -| - -| - -|
| 2 个|三 个|第 0 页|第 0 页|第 0 页|
| 四 个|十三|第 0 页|第 0 页|第 0 页|
| 五 个|一 个|第 0 页|第 0 页|第 0 页|
| 三 个|五 个|第 0 页|第 0 页|一 个|
| 第 0 页|四 个|第 0 页|第 0 页|八 个|
想要 的 结果 :
| 状态|一 个|2 个|三 个|五 个|总计|
| - -| - -| - -| - -| - -| - -|
| 2 个|三 个|第 0 页|第 0 页|第 0 页|三 个|
| 四 个|十三|第 0 页|第 0 页|第 0 页|十三|
| 五 个|一 个|第 0 页|第 0 页|第 0 页|一 个|
| 三 个|五 个|第 0 页|第 0 页|一 个|六 个|
| 第 0 页|四 个|第 0 页|第 0 页|八 个|十二|
| 状态 2 、 4 、 5 的 总和|十七 岁|第 0 页|第 0 页|第 0 页|十七 岁|
| 所有 状态 的 总和|二十六 人|第 0 页|第 0 页|第 0 页|三十五|
我 试 着 补充 一 句 :

Select STATUS,USER_TYPE,
       count(user_type) as records,
       sum(user_type) over (partition by status) as total

格式
最 后 :

pivot ( sum (records) for user_type in (1,2,3,5)) pivot_table

格式
但 从 逻辑 上 讲 我 仍然 不在 那里 。

5gfr0r5j

5gfr0r5j1#

您希望在分组集上使用GROUP BY CUBE和条件聚集和过滤器:

SELECT CASE GROUPING_ID(status, CASE WHEN status IN (2, 4, 5) THEN 1 ELSE 0 END)
       WHEN 0
       THEN TO_CHAR(status)
       WHEN 2
       THEN 'SUB-TOTAL'
       ELSE 'TOTAL'
       END AS status,
       COUNT(CASE user_type WHEN 1 THEN 1 END) AS "1",
       COUNT(CASE user_type WHEN 2 THEN 1 END) AS "2",
       COUNT(CASE user_type WHEN 3 THEN 1 END) AS "3",
       COUNT(CASE user_type WHEN 5 THEN 1 END) AS "5",
       COUNT(*) AS total
FROM   table_name
GROUP BY CUBE(status, CASE WHEN status IN (2, 4, 5) THEN 1 ELSE 0 END)
HAVING GROUPING_ID(status, CASE WHEN status IN (2, 4, 5) THEN 1 ELSE 0 END) IN (0, 3)
OR     (   GROUPING_ID(status, CASE WHEN status IN (2, 4, 5) THEN 1 ELSE 0 END) = 2
       AND CASE WHEN status IN (2, 4, 5) THEN 1 ELSE 0 END = 1 )

对于示例数据(表示多个联接表的输出):

CREATE TABLE table_name (status, user_type) AS
  SELECT 2, 1 FROM DUAL CONNECT BY LEVEL <=  3 UNION ALL
  SELECT 4, 1 FROM DUAL CONNECT BY LEVEL <= 13 UNION ALL
  SELECT 5, 1 FROM DUAL CONNECT BY LEVEL <=  1 UNION ALL
  SELECT 3, 1 FROM DUAL CONNECT BY LEVEL <=  5 UNION ALL
  SELECT 3, 5 FROM DUAL CONNECT BY LEVEL <=  1 UNION ALL
  SELECT 0, 1 FROM DUAL CONNECT BY LEVEL <=  4 UNION ALL
  SELECT 0, 5 FROM DUAL CONNECT BY LEVEL <=  8;

输出:
| 状态|一个|2个|三个|五个|总计|
| - -|- -|- -|- -|- -|- -|
| 第0页|四个|第0页|第0页|八个|十二|
| 三个|五个|第0页|第0页|一个|六个|
| 2个|三个|第0页|第0页|第0页|三个|
| 四个|十三|第0页|第0页|第0页|十三|
| 五个|一个|第0页|第0页|第0页|一个|
| 小计|十七岁|第0页|第0页|第0页|十七岁|
| 总计|二十六人|第0页|第0页|九个|三十五|
您可以更改字符串文字以匹配所需输出的行标题。
fiddle

y4ekin9u

y4ekin9u2#

这样做的一个选项是使用MODEL子句,如下所示:

Select 
    *
From
    (
        Select Cast(p.STATUS as VARCHAR2(30)) "STATUS",  p."1", p."2", p."3", p."5"
        From pivot_table p Union All
        Select 'Sum of Statuses 2, 4, 5', Null, Null, Null, Null From Dual Union All
        Select 'Sum of All Statuses', Null, Null, Null, Null From Dual
    ) 
MODEL
    Dimension By      (STATUS)
    Measures          ("1", "2", "3", "5", 0 "TOTAL")
    Rules     
          (
              "1"['Sum of All Statuses'] = Sum("1")[ANY],     -- Grand Total of Column 1 = Sum(1) for ANY Status (Dimension)
              "2"['Sum of All Statuses'] = Sum("2")[ANY],
              "3"['Sum of All Statuses'] = Sum("3")[ANY],
              "5"['Sum of All Statuses'] = Sum("5")[ANY],
              --
              "1"['Sum of Statuses 2, 4, 5'] = Sum("1")[STATUS IN('2', '4', '5')],    -- SubTotal of Column 1 = Sum(1) for Status IN 2, 4, 5
              "2"['Sum of Statuses 2, 4, 5'] = Sum("2")[STATUS IN('2', '4', '5')],
              "3"['Sum of Statuses 2, 4, 5'] = Sum("3")[STATUS IN('2', '4', '5')],
              "5"['Sum of Statuses 2, 4, 5'] = Sum("5")[STATUS IN('2', '4', '5')],
              --
              TOTAL[ANY] = "1"[CV()] + "2"[CV()] + "3"[CV()] + "5"[CV()]      -- for each row do the addition of columns with the same Current Value 'CV()' of Status
          )

根据您问题中的数据:

WITH
    pivot_table AS
        (   
       Select 2 "STATUS", 3 "1", 0 "2", 0 "3", 0 "5" From Dual Union All
       Select 4 "STATUS", 13 "1", 0 "2", 0 "3", 0 "5" From Dual Union All
       Select 5 "STATUS", 1 "1", 0 "2", 0 "3", 0 "5" From Dual Union All
       Select 3 "STATUS", 5 "1", 0 "2", 0 "3", 1 "5" From Dual Union All
       Select 0 "STATUS", 4 "1", 0 "2", 0 "3", 8 "5" From Dual 
        )

结果应该是:
| 状态|一个|2个|三个|五个|总计|
| - -|- -|- -|- -|- -|- -|
| 2个|三个|第0页|第0页|第0页|三个|
| 四个|十三|第0页|第0页|第0页|十三|
| 五个|一个|第0页|第0页|第0页|一个|
| 三个|五个|第0页|第0页|一个|六个|
| 第0页|四个|第0页|第0页|八个|十二|
| 状态2、4、5的总和|十七岁|第0页|第0页|第0页|十七岁|
| 所有状态的总和|二十六人|第0页|第0页|九个|三十五|
此致

相关问题