我有一张包含以下2个相关字段的表格: -
QUOTEID REG_DATE
001 02/10/2017
002 09/10/2017
003 10/10/2017
004 13/10/2017
005 13/10/2017
006 17/10/2017
是否可以让Oracle输出上述内容,但按星期(星期六到星期五)分组并计算,列名是相关星期五的日期?因此,使用上面的数据,输出将是: -
20/10/2017 13/10/2017 06/10/2017
1 5 1
答案 0 :(得分:1)
关于动态数据透视已经有很多问题,例如:Dynamic pivot in oracle sql。
此查询仅演示如何查找"相关星期五"如果您知道所有日期,则转动数据:
select *
from (select quoteid,
to_char(trunc(reg_date + 2, 'iw') + 4, 'dd/mm/yyyy') friday
from t)
pivot (count(quoteid) for friday in ('20/10/2017', '13/10/2017', '06/10/2017'))
测试:
with t(quoteid, reg_date) as (
select '001', date '2017-10-02' from dual union all
select '002', date '2017-10-09' from dual union all
select '003', date '2017-10-10' from dual union all
select '004', date '2017-10-13' from dual union all
select '005', date '2017-10-13' from dual union all
select '006', date '2017-10-17' from dual)
select *
from (select quoteid,
to_char(trunc(reg_date + 2, 'iw') + 4, 'dd/mm/yyyy') friday
from t)
pivot (count(quoteid) for friday in ('20/10/2017', '13/10/2017', '06/10/2017'))
结果:
'20/10/2017' '13/10/2017' '06/10/2017'
------------ ------------ ------------
1 4 1