查询整个月的周数并根据该月显示内容

时间:2015-02-06 04:08:51

标签: sql oracle date

我对我的分析编程有疑问。我已经有一个查询来显示一年中的几周。但是,当我需要总结每周发生的价值时,我会陷入困境。所以这是我的SQL代码,

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, 'DD-MM-YYYY') START_WEEK_DATE,
       TO_CHAR (start_date + (LEVEL) * 7, 'DD-MM-YYYY') END_WEEK_DATE,           
            (SELECT SUM(ONSITE_UPD_QTY) FROM DTL_ERC_UPD@WELTES_SITEMON_LINK WHERE UPD_DATE BETWEEN
                TO_CHAR (start_date + (LEVEL) * 7, 'MM/DD/YYYY') AND TO_CHAR (start_date + (LEVEL) * 7, 'MM/DD/YYYY') CONNECT BY start_date + (LEVEL - 1) * 7 < end_date)
      FROM (SELECT TO_DATE ('01/01/2015', 'MM/DD/YYYY') start_date,
                   TO_DATE ('12/31/2015', 'MM/DD/YYYY') end_date
              FROM DUAL)
CONNECT BY start_date + (LEVEL - 1) * 7 < end_date; 

它会显示几周,但是当我将这部分添加到我现在的中间时,

(SELECT SUM(ONSITE_UPD_QTY) FROM DTL_ERC_UPD@WELTES_SITEMON_LINK WHERE UPD_DATE BETWEEN
                    TO_CHAR (start_date + (LEVEL) * 7, 'MM/DD/YYYY') AND TO_CHAR (start_date + (LEVEL) * 7, 'MM/DD/YYYY') CONNECT BY start_date + (LEVEL - 1) * 7 < end_date)

它会抛出这些错误, ORA-01843:不是有效月份 ORA-02063:来自WELTES_SITEMON_LINK的前一行

因此对于DTL_ERC_UPD,我有

ONSITE_UPD_QTY    UPD_DATE
1                 2/5/2015 12:00:01 AM
1                 2/5/2015 12:00:01 AM
1                 2/4/2015
1                 2/4/2015
1                 2/4/2015

我希望它会在1月1日至8日期间显示5,而在其余部分则显示0。

请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

下面是一个样本表

select cast('01/08/2013' as Date) dte INTO #temp union all select
'03/01/2013' union all select
'11/01/2013' union all select
'12/01/2013' union all select
'10/21/2014' union all select
'10/27/2014' union all select
'10/30/2014' union all select
'10/31/2014' union all select
'11/01/2014' union all select
'11/02/2014' union all select
'11/04/2014' union all select
'11/05/2014' union all select
'11/08/2014' union all select
'11/09/2014' union all select
'11/11/2014' union all select
'11/20/2014' union all select
'11/07/2014' union all select
'07/11/2014' union all select
'11/13/2013' union all select
'09/01/2014' union all select
'11/03/2014' union all select
'11/18/2014' union all select
'12/05/2014' union all select
'07/24/2014' union all select
'07/26/2014' union all select
'07/27/2014' union all select
'07/28/2014' union all select
'07/29/2014' union all select
'07/30/2014' union all select
'01/01/2014' union all select
'02/01/2014' union all select
'04/01/2014' union all select
'05/01/2014' union all select
'06/01/2014' union all select
'06/01/2014' union all select
'07/01/2014' union all select
'07/01/2014' union all select
'11/05/2013' union all select
'06/16/2014' union all select
'06/17/2014' union all select
'06/18/2014' union all select
'06/19/2014' union all select
'06/20/2014' union all select
'06/21/2014' union all select
'06/22/2014' union all select
'06/23/2014' union all select
'06/24/2014' union all select
'06/25/2014' union all select
'06/26/2014' union all select
'06/27/2014' union all select
'06/28/2014' union all select
'06/29/2014'

现在您可以获得一周的周计数开始日期和一周的结束日期

select count([Week]) cont,[Week],
DATEADD(Day,(cast(SUBSTRING([Week],5,LEN( [Week])) as integer)*7)-7,
DATEADD(year,cast(SUBSTRING([Week],1,4) as integer)-1900,0)) startDte,
DATEADD(Day,cast(SUBSTRING([Week],5,LEN( [Week])) as integer)*7,
DATEADD(year,cast(SUBSTRING([Week],1,4) as integer)-1900,0)) endDte
from (select cast(datepart(YYYY,cast(dte as DATE)) as varchar(4))+''+cast(datepart(WW,cast(dte as DATE)) as varchar(2)) [Week],dte from #temp 
 ) AS temp GROUP BY [Week]

输出

cont    startDte              endDte

1   2013-01-08 00:00:00.000 2013-01-15 00:00:00.000
1   2013-10-29 00:00:00.000 2013-11-05 00:00:00.000
1   2013-11-05 00:00:00.000 2013-11-12 00:00:00.000
1   2013-11-12 00:00:00.000 2013-11-19 00:00:00.000
1   2013-12-03 00:00:00.000 2013-12-10 00:00:00.000
1   2013-02-26 00:00:00.000 2013-03-05 00:00:00.000
1   2014-01-01 00:00:00.000 2014-01-08 00:00:00.000
1   2014-04-02 00:00:00.000 2014-04-09 00:00:00.000
1   2014-04-30 00:00:00.000 2014-05-07 00:00:00.000
2   2014-06-04 00:00:00.000 2014-06-11 00:00:00.000
6   2014-06-18 00:00:00.000 2014-06-25 00:00:00.000
7   2014-06-25 00:00:00.000 2014-07-02 00:00:00.000
3   2014-07-02 00:00:00.000 2014-07-09 00:00:00.000
1   2014-07-09 00:00:00.000 2014-07-16 00:00:00.000
2   2014-07-23 00:00:00.000 2014-07-30 00:00:00.000
4   2014-07-30 00:00:00.000 2014-08-06 00:00:00.000
1   2014-09-03 00:00:00.000 2014-09-10 00:00:00.000
1   2014-10-22 00:00:00.000 2014-10-29 00:00:00.000
4   2014-10-29 00:00:00.000 2014-11-05 00:00:00.000
6   2014-11-05 00:00:00.000 2014-11-12 00:00:00.000
2   2014-11-12 00:00:00.000 2014-11-19 00:00:00.000
2   2014-11-19 00:00:00.000 2014-11-26 00:00:00.000
1   2014-12-03 00:00:00.000 2014-12-10 00:00:00.000
1   2014-01-29 00:00:00.000 2014-02-05 00:00:00.000

答案 1 :(得分:0)

代码中的第二个connect by是不必要的。使用适当的join在逻辑部分中组织查询。如上所述,不要使用date to char转换进行比较。以下是您可以使用的示例查询:

with period as (
  select to_date('02/01/2015', 'MM/DD/YYYY') start_date, 
      to_date('03/01/2015', 'MM/DD/YYYY') end_date from dual),
weeks as ( 
  select level week_num_inc,
    to_char(start_date + (level - 1) * 7, 'WW') week_position,
    start_date + (level - 1) * 7 start_week_date,
    start_date + level * 7 end_week_date
  from period
  connect by start_date + (level - 1) * 7 < end_date)
select week_num_inc, week_position, start_week_date, end_week_date,
    nvl(sum(dtl_erc_upd.onsite_upd_qty), 0) quantity
  from weeks w
    left join dtl_erc_upd on start_week_date <= upd_date 
      and upd_date < end_week_date
  group by week_num_inc, week_position, start_week_date, end_week_date
  order by start_week_date

result:
WEEK_NUM_INC  WEEK_POSITION  START_WEEK_DATE  END_WEEK_DATE  QUANTITY
           1  05             2015-02-01       2015-02-08            5
           2  06             2015-02-08       2015-02-15            0
           3  07             2015-02-15       2015-02-22            0
           4  08             2015-02-22       2015-03-01            0