我正在尝试使用来自oracle的数据进行S曲线分析,该数据跨越指定的日期范围,
我的查询看起来像这样,
SELECT LEVEL WEEK_NUM_INCR,
TO_CHAR (start_date + (LEVEL - 1) * 7, 'WW') WEEK_POSITION,
/* WEEK POSITION FOR THE WHOLE YEAR */
TO_CHAR (start_date + (LEVEL - 1) * 7, 'MM/DD/YYYY') START_WEEK_DATE,
TO_CHAR (start_date + (LEVEL) * 7, 'MM/DD/YYYY') END_WEEK_DATE,
(SELECT SUM (ONSITE_UPD_QTY)
FROM DTL_ERC_UPD@WELTES_SITEMON_LINK
WHERE UPD_DATE BETWEEN RS.start_date AND RS.end_date
AND PROJECT_NAME IN (SELECT PROJECT_NAME
FROM PROJECT
WHERE PROJECT_NO = RS.JOB_NAME))
ONSITEQTY
FROM (SELECT PS.PROJECT_START_DT start_date,
PS.JOB_NAME JOB_NAME,
PS.PROJECT_END_DT end_date
FROM PROJECT_SPAN PS
WHERE PS.JOB_NAME = 'W-14026') RS
CONNECT BY start_date + (LEVEL - 1) * 7 < end_date;
所以在PROJECT_SPAN表中,内容是,
PROJECT_START_DT PROJECT_END_DT JOB_NAME
1/1/2015 2/1/2015 W-14026
在DTL_ERC_UPD @ WELTES_SITEMON_LINK上,内容为,
PROJECT_NAME UPD_DATE ONSITE_UPD_QTY
---------------------------------------------------
SMSMILLHOUSE 2/5/2015 12:00:01 AM 2
SMSSUGARWHOUSE 2/5/2015 12:00:01 AM 2
SMSMILLHOUSE 2/4/2015 12:00:01 AM 4
SMSMILLHOUSE 2/4/2015 12:00:01 AM 6
IGGMILLHOUSE 2/4/2015 12:00:01 AM 4
IGGMILLHOUSE 2/4/2015 12:00:01 AM 6
因此,连接表是包含
的PROJECTPROJECT_NO PROJECT_NAME
-----------------------
W-14026 SMSMILLHOUSE
W-14026 SMSSUGARWHOUSE
当我查询sql时我只得到这个
WEEK_NUM_INCR WEEK_POSITION START_WEEK_DATE END_WEEK_DATE ONSITEQTY
1.00 01 01/01/2015 01/08/2015
2.00 02 01/08/2015 01/15/2015
3.00 03 01/15/2015 01/22/2015
4.00 04 01/22/2015 01/29/2015
5.00 05 01/29/2015 02/05/2015
预期输出
WEEK_NUM_INCR WEEK_POSITION START_WEEK_DATE END_WEEK_DATE ONSITEQTY
1.00 01 01/01/2015 01/08/2015 0
2.00 02 01/08/2015 01/15/2015 0
3.00 03 01/15/2015 01/22/2015 0
4.00 04 01/22/2015 01/29/2015 0
5.00 05 01/29/2015 02/05/2015 14
请帮助我,我的查询缺少什么?
非常感谢
答案 0 :(得分:1)
为什么你认为WEEK_NUM_INCR = 5时ONSITEQTY为14?
DTL_ERC_UPD包含2月4日的两行和第5行的两行(由于PROJECT_NAME限制,其余两行不适合)。
PROJECT_SPAN中的START_DATE 和 END_DATE 仅包括1月 - 没有适合“介于”条件的行。您似乎期望DTL_ERC_UPD在 START_WEEK_DATE 和 END_WEEK_DATE 之间受到限制。如果是这种情况,那么您需要将查询换一个以下选择:
with PROJECT as (
select 'W-14026' as PROJECT_NO, 'SMSMILLHOUSE' as PROJECT_NAME from dual union all
select 'W-14026', 'SMSSUGARWHOUSE' from dual
),
DTL_ERC_UPD as (
select 'SMSMILLHOUSE' as PROJECT_NAME, to_date( '2/5/2015 12:00:01 AM', 'mm/dd/yyyy hh:mi:ss AM' ) as UPD_DATE, 2 as ONSITE_UPD_QTY from dual union all
select 'SMSSUGARWHOUSE', to_date( '2/5/2015 12:00:01 AM', 'mm/dd/yyyy hh:mi:ss AM' ), 2 from dual union all
select 'SMSMILLHOUSE', to_date( '2/4/2015 12:00:01 AM', 'mm/dd/yyyy hh:mi:ss AM' ), 4 from dual union all
select 'SMSMILLHOUSE', to_date( '2/4/2015 12:00:01 AM', 'mm/dd/yyyy hh:mi:ss AM' ), 6 from dual union all
select 'IGGMILLHOUSE', to_date( '2/4/2015 12:00:01 AM', 'mm/dd/yyyy hh:mi:ss AM' ), 4 from dual union all
select 'IGGMILLHOUSE', to_date( '2/4/2015 12:00:01 AM', 'mm/dd/yyyy hh:mi:ss AM' ), 6 from dual
),
PROJECT_SPAN as (
select to_date( '1/1/2015', 'mm/dd/yyyy' ) as PROJECT_START_DT, to_date( '2/1/2015', 'mm/dd/yyyy' ) as PROJECT_END_DT, 'W-14026' as JOB_NAME from dual
)
select WEEK_NUM_INCR,
TO_CHAR (START_WEEK_DATE, 'WW') WEEK_POSITION,
/* WEEK POSITION FOR THE WHOLE YEAR */
TO_CHAR (START_WEEK_DATE, 'MM/DD/YYYY') START_WEEK_DATE,
TO_CHAR (END_WEEK_DATE, 'MM/DD/YYYY') END_WEEK_DATE,
(SELECT SUM (ONSITE_UPD_QTY)
FROM DTL_ERC_UPD
WHERE UPD_DATE BETWEEN rs.START_WEEK_DATE AND rs.END_WEEK_DATE
AND PROJECT_NAME IN (SELECT PROJECT_NAME
FROM PROJECT
WHERE PROJECT_NO = RS.JOB_NAME)) ONSITEQTY
from ( SELECT LEVEL WEEK_NUM_INCR,
start_date + (LEVEL - 1) * 7 START_WEEK_DATE,
start_date + (LEVEL) * 7 END_WEEK_DATE,
RS.JOB_NAME
FROM (SELECT PS.PROJECT_START_DT start_date,
PS.JOB_NAME JOB_NAME,
PS.PROJECT_END_DT end_date
FROM PROJECT_SPAN PS
WHERE PS.JOB_NAME = 'W-14026') RS
CONNECT BY start_date + (LEVEL - 1) * 7 < end_date ) rs
但无论如何,ONSITEQTY将是10而不是14,因为上周在2月5日00:00:00结束。两行DTL_ERC_UPD的UPD_DATE还有一分钟。它们在上周不合适。