Oracle SQL 多列与单个 range/legend 对齐

Oracle SQL multiple columns aligned to a single range/legend

我的申请中有一个 activity table。它有一个项目 ID、一个 activity ID、一个基准完成日期、一个实际完成日期、一个计划完成日期和一个最晚完成日期。

我想在每个日期列中计算项目每周的 activity 个 ID。然后我想获取这些日期列中的每一个并将它们相互绘制。

最终结果将是一条曲线,显示第 2 周的活动数量与第 2 周的实际活动数量与第 2 周计划活动数量的比较以及延迟完成的活动数量第 2 周

我可以这样做来显示数据...

SELECT ACTIVITY.PROJECTOBJECTID,
       ACTIVITY.OBJECTID,
       TRUNC(ACTIVITY.BASELINEFINISHDATE,'W')+7 AS BLFIN,
       TRUNC(ACTIVITY.ACTUALFINISHDATE,'W')+7 AS AFIN,
       TRUNC(ACTIVITY.FINISHDATE,'W')+7 AS FIN,
       TRUNC(ACTIVITY.REMAININGLATEFINISHDATE,'W')+7 AS LFIN
  FROM PXRPTUSER.ACTIVITY ACTIVITY
 WHERE (ACTIVITY.PROJECTOBJECTID = :POID)

但是,现在我有四个日期列。理想情况下,我想要四个计数列和一个日期列。这是我必须处理的数据示例。

PID     AID     BLFIN     AFIN  FIN         LFIN
39987   5874494 2/22/2015       2/22/2015   6/15/2015
39987   5874495 2/22/2015       2/22/2015   6/15/2015
39987   5874496 2/22/2015       2/22/2015   6/15/2015
39987   5874497 2/22/2015       2/22/2015   6/15/2015
39987   5874498 2/22/2015       2/22/2015   6/15/2015
39987   5874499 4/22/2015       4/22/2015   6/15/2015
39987   5874500 2/22/2015       2/22/2015   6/15/2015
39987   5874501 2/22/2015       2/22/2015   6/15/2015
39987   5874502 4/8/2015        4/8/2015    6/15/2015
39987   5874503 4/8/2015        4/8/2015    6/15/2015
39987   5874504 4/8/2015        4/8/2015    6/15/2015
39987   5874505 5/15/2015       5/15/2015   6/15/2015
39987   5874506 5/15/2015       5/15/2015   6/15/2015
39987   5874507 4/8/2015        4/8/2015    6/15/2015
39987   5874508 4/8/2015        4/8/2015    6/15/2015
39987   5874509 4/8/2015        4/8/2015    6/15/2015
39987   5874510 4/8/2015        4/8/2015    6/15/2015
39987   5874511 4/8/2015        4/8/2015    6/15/2015
39987   5874512 4/8/2015        4/8/2015    6/15/2015
39987   5874513 4/8/2015        4/8/2015    6/15/2015
39987   5874514 4/8/2015        4/8/2015    6/15/2015
39987   5874515 4/8/2015        4/8/2015    6/15/2015
39987   5874516 4/8/2015        4/8/2015    6/15/2015
39987   5874517 4/8/2015        4/8/2015    6/15/2015
39987   5874537 2/22/2015       2/22/2015   6/15/2015
39987   5874538 2/22/2015       2/22/2015   6/15/2015
39987   5874539 2/22/2015       2/22/2015   6/15/2015
39987   5874540 2/22/2015       2/22/2015   2/22/2015
39987   5874542 2/22/2015       2/22/2015   6/15/2015
39987   5874543 2/22/2015       2/22/2015   6/15/2015
39987   5874544 2/22/2015       2/22/2015   6/15/2015
39987   5874545 2/22/2015       2/22/2015   6/15/2015
39987   5874546 2/22/2015       2/22/2015   6/15/2015
39987   5874547 2/22/2015       2/22/2015   6/15/2015
39987   5874548 2/22/2015       2/22/2015   6/15/2015
39987   5874549 2/22/2015       2/22/2015   6/15/2015
39987   5874550 2/22/2015       2/22/2015   6/15/2015
39987   5874551 2/22/2015       2/22/2015   6/15/2015
39987   5874552 2/22/2015       2/22/2015   6/15/2015
39987   5874553 2/22/2015       2/22/2015   6/15/2015
39987   5874554 2/22/2015       2/22/2015   6/15/2015

下面的查询给出了描述的结果。 WKN 列是 week number in ISO standard,您可能想将其更改为其他内容,例如。 'WW'。 此外,如果您有不同年份的日期,则必须在查询中连接年份(在 bf、af、pf 和 lf 子查询中将年份附加到 wkn)。

with 
bf as (select to_char(blfin, 'iw') wkn, count(aid) cnt 
  from activity group by to_char(blfin, 'iw')),
af as (select to_char(afin, 'iw') wkn, count(aid) cnt 
  from activity group by to_char(afin, 'iw')),
pf as (select to_char(fin, 'iw') wkn, count(aid) cnt 
  from activity group by to_char(fin, 'iw')),
lf as (select to_char(lfin, 'iw') wkn, count(aid) cnt 
  from activity group by to_char(lfin, 'iw'))
select wkn, nvl(bf.cnt, 0) as baseline, nvl(af.cnt, 0) as actual, 
    nvl(pf.cnt, 0) as planned, nvl(lf.cnt, 0) as late
  from bf
    full join af using (wkn)
    full join pf using (wkn)
    full join lf using (wkn)
  where wkn is not null
  order by wkn

结果:

  WKN BASELINE ACTUAL   PLANNED  LATE
  --- -------- -------- -------- --------
  08        24        0       24        1
  15        14        0       14        0
  17         1        0        1        0
  20         2        0        2        0
  25         0        0        0       40