我有一些代码
select
'WEEK '|| row_number() over(order by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'SATURDAY'), 'IW')) WEEK,
count(*)
from opencall
where trunc(to_date(substr(LOGDATE,1,10), 'DD-MM-YY')) BETWEEN '01-JAN-14' AND '31-DEC-14'
group by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'SATURDAY'), 'IW')
order by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'SATURDAY'), 'IW');
另一个stackoverflow chap为我编写了一个很棒的代码,用于从数据库中提取调用并按周计算/分组它们,如果每周都有调用,它就能很好地工作。
但是我尝试将此代码重新用于其他用途,如果在给定的一周内没有结果,我会得到更少的分组,例如,可能只返回37周而不是当前的50周。
由于这会消除没有电话的周数,因此周数不等于一年中的实际周数。所以我实际上并不知道从几个星期开始按顺序返回来的几周,而不是在没有电话的几周内零。
如果给定的一周没有返回结果,有没有办法返回零值?我想这可能会使代码变得更加复杂。
如果我在一周内使用此代码
e.g:
WEEK 1 231
WEEK 2 657
WEEK 3 529
WEEK 4 606
WEEK 5 594
WEEK 6 517
WEEK 7 683
WEEK 8 664
WEEK 9 583
WEEK 10 740
答案 0 :(得分:2)
如果你想知道你在哪个星期展示,那么为什么用row_number()大惊小怪?
这为您提供以周为单位的年份,从第1周开始的前七天开始,依此类推:
select to_char(mydate, 'ww'), count(*)
from
(
select to_date(substr(logdate,1,10), 'dd-mm-yy') as mydate
from opencall
)
where extract(year from mydate) = 2014
group by to_char(mydate, 'ww')
order by to_char(mydate, 'ww');
(为什么LOGDATE是一个字符串?为什么它的前10个字符的格式为'DD-MM-YY',长度为8个字符?)
答案 1 :(得分:0)
select
'WEEK '|| row_number() over(order by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'SATURDAY'), 'IW')) WEEK,
row_number() over(order by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'SATURDAY'), 'IW')) mWeek,
count(*)
from (SELECT to_char(SYSDATE - (LEVEL * 7),'IW') TheDate
FROM dual
CONNECT BY LEVEL <= 52) WoY
LEFT JOIN opencall
on Woy.TheDate = OpenCall.mWeek
where trunc(to_date(substr(LOGDATE,1,10), 'DD-MM-YY')) BETWEEN '01-JAN-14' AND '31-DEC-14'
group by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'SATURDAY'), 'IW')
order by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'SATURDAY'), 'IW');
这样的事可能有用。
答案 2 :(得分:0)
我用测试数据尝试了类似的东西并且有效。我希望这也适合你:
select
'WEEK '|| row_number() over(order by nvl(to_char(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY') ,'SATURDAY'), 'IW'),week)) WEEK
,count(wk)
from (select t.*,to_char(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'IW') wk,w.week from
table1 t right join (SELECT lpad(level,2,0) week
FROM dual
CONNECT BY LEVEL <= 52 ) w on (to_char(to_date(substr(t.LOGDATE,1,10), 'DD-MM-YY'),'IW') = w.week)
order by w.week)
where trunc(to_date(substr(LOGDATE,1,10), 'DD-MM-YY')) BETWEEN '01-JAN-14' AND '31-DEC-14'
group by nvl(to_char(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY') ,'SATURDAY'), 'IW'),week)
;
答案 3 :(得分:-1)
- 在架构控制器下创建一个测试表,并在周数倍数为4的所有日子上插入数据。
create table alber.week_table (mydate date, mydescription varchar2(100));
declare
cur_date date := to_date('01/01/'||to_char(sysdate,'yyyy'),'dd/mm/yyyy') ;
week number := 01;
begin
EXECUTE IMMEDIATE 'TRUNCATE TABLE alber.week_table';
while (cur_date < sysdate) loop
if mod(week,4) = 0 then
insert into alber.week_table values(cur_date, to_char(sysdate, 'day dd month yyyy hh24:mi:ss')) ;
end if;
cur_date := cur_date + 1;
week := to_number(to_char(cur_date,'IW'));
end loop;
end;
- 然后一个可能的解决方案是:
select theweek "Week", sum( decode( nvl(mydescription,0),'0',0,1) ) "Num of Records"
FROM
(
SELECT to_char(SYSDATE - (LEVEL * 7),'IW') theweek
FROM dual
CONNECT BY LEVEL <= 52
) w
LEFT JOIN week_table ON w.theweek = to_char(mydate,'IW')
group by theweek
order by 1;
结果
Week Num of Records
---- ----------------------
01 0
02 0
03 0
04 7
05 0
06 0
07 0
08 7
09 0
10 0
11 0
12 7
13 0
14 0
15 0
16 7
17 0
18 0
.
.
.