如何在 Pivot 子句中使用 Count

问题描述 投票:0回答:1

我正在使用 Oracle 21C。
我的查询涉及三个表,可以使用以下代码创建:

Create Table vol_att
( prim_key integer NOT NULL,
 event_fkey integer,
 contact_fkey integer,
 attendance_type varchar2(256),
 CONSTRAINT vol_att_pk PRIMARY KEY (prim_key)
);
Insert All
   Into vol_att(prim_key, event_fkey, contact_fkey, attendance_type)
   Values(1, 1, 801, 'I')
   Into vol_att(prim_key, event_fkey, contact_fkey, attendance_type)
   Values(2, 1, 234, 'Z')
   Into vol_att(prim_key, event_fkey, contact_fkey, attendance_type)
   Values(3, 2, 258, 'I')
   Into vol_att(prim_key, event_fkey, contact_fkey, attendance_type)
   Values(4, 2, 234, 'Z')
   Select 1 from DUAL;
   
Create Table vol_con
( prim_key integer NOT NULL,
 last_name varchar2(256),
 first_name varchar2(256),
 CONSTRAINT vol_con_pk PRIMARY KEY (prim_key)
);
Insert All
   Into vol_con(prim_key, last_name, first_name)
   Values(234, 'Potter', 'Harry')
   Into vol_con(prim_key, last_name, first_name)
   Values(258, 'Weasley', 'Ron')
   Into vol_con(prim_key, last_name, first_name)
   Values(801, 'Granger', 'Hermione')
   Into vol_con(prim_key, last_name, first_name)
   Values(500, 'Malfoy', 'Draco')
   Select 1 from DUAL;
   
CREATE TABLE vol_evn
( prim_key integer NOT NULL,
 event_date date,
 CONSTRAINT vol_evn_pk PRIMARY KEY (prim_key)
);
Insert All
   Into vol_evn(prim_key, event_date)
   Values(1, to_date('30-mar-2023', 'dd-mon-yyyy'))
   Into vol_evn(prim_key, event_date)
   Values(2, to_date('18-apr-2023', 'dd-mon-yyyy'))
   Select 1 from DUAL;

以下代码生成报告

select
    *
from
    (select
        vc.last_name
      , va.attendance_type
      , ve.event_date
    from
        vol_att va
            full outer join vol_evn ve
              on va.event_fkey = ve.prim_key
            full outer join vol_con vc
              on va.contact_fkey = vc.prim_key
    )
pivot (
    max(attendance_type) for event_date in 
        (to_date('2023-03-30', 'yyyy-mm-dd') Mar_30_2023, 
         to_date('2023-04-18', 'yyyy-mm-dd') Apr_18_2023
        )
      )
order by
    last_name;

上面的代码产生如下所示的预期结果:

LAST_NAME 2023 年 3 月 30 日 2023 年 4 月 18 日
格兰杰 (空)
马尔福 (空) (空)
波特 Z Z
韦斯莱 (空)

我想制作一份新报告来计算出席人数(任何不为空的内容)。 我希望新报告看起来像:

2023 年 3 月 30 日 2023 年 4 月 18 日
2 2

如何更改我的代码,以便它生成两行报告:日期标题和包含计数的第二行? 感谢您查看此内容。

count pivot
1个回答
0
投票

您可以将查询用作公用表表达式,然后从中选择您需要的计数:

with crosstab as (<your first query>)
select sum(case when Mar_30_2023 is null then 0 else 1 end) Mar_30_2023,
       sum(case when Apr_18_2023 is null then 0 else 1 end) Apr_18_2023
from   crosstab;

完整的:

with crosstab as (
select
    *
from
    (select
        vc.last_name
      , va.attendance_type
      , ve.event_date
    from
        vol_att va
            full outer join vol_evn ve
              on va.event_fkey = ve.prim_key
            full outer join vol_con vc
              on va.contact_fkey = vc.prim_key
    )
pivot (
    max(attendance_type) for event_date in 
        (to_date('2023-03-30', 'yyyy-mm-dd') Mar_30_2023, 
         to_date('2023-04-18', 'yyyy-mm-dd') Apr_18_2023
        )
      )
order by
    last_name)
select sum(case when Mar_30_2023 is null then 0 else 1 end) Mar_30_2023,
       sum(case when Apr_18_2023 is null then 0 else 1 end) Apr_18_2023
from   crosstab;
© www.soinside.com 2019 - 2024. All rights reserved.