为 S 曲线目的查询数据

Querying data for S-Curve Purposes

我正在尝试使用跨越指定日期范围的 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_SPANtable的内容是,

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

所以连接的 table 是包含

的项目
PROJECT_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

请帮我查询一下我遗漏了什么???

非常感谢

为什么 WEEK_NUM_INCR = 5 时 ONSITEQTY 为 14?

DTL_ERC_UPD 包含两行 2 月 4 日和两行 5 日(由于 PROJECT_NAME 限制,其余两行不适合)。

START_DATE 和 PROJECT_SPAN 中的 END_DATE 仅包括一月 - 没有行适合 "between" 条件。您似乎希望 DTL_ERC_UPD 被限制在 START_WEEK_DATEEND_WEEK_DATE 之间。如果是这种情况,那么您需要将查询再包装一个 select,如下所示:

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 多一分钟。他们上周不适合。