Oracle SQL multiple columns aligned to a single range/legend

喜夏-厌秋 提交于 2019-12-13 06:52:25

问题


I have an activity table in my application. It has a project ID, an activity ID, a baseline finish date, an actual finish date a planned finish date and a late finish date.

I want to take the count of activity ID for each week of the project in each date column. I then want to take each of these date columns and plot them against each other.

The end result would be a curve that shows the number of activities in week 2 compared to the actual number of activities in week 2 compared to the number of planned activities in week two and the number of late-finish activities in week 2.

I can do this to get the data to show up...

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)

But, now I have four date columns. Ideally I want four count columns with a single date column. Here is a sample of the data I have to work with.

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

回答1:


Query below gives described results. Column WKN is week number in ISO standard, you may want to change it to something else, eg. 'WW'. Also if you have dates from different years you have to concatenate year in query (attach year to wkn in bf, af, pf and lf subqueries).

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

Results:

  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


来源:https://stackoverflow.com/questions/28652040/oracle-sql-multiple-columns-aligned-to-a-single-range-legend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!