#Oracle 我有一个与日期相关的记录,我会在两个日期D1和D2之间按周分组。
我的会议桌是,
id_Meeting | meeting_date -----------+-------------- 1 | 07/04/2017 2 | 10/06/2017 3 | 17/04/2017 4 | 08/05/2017 5 | 19/04/2017 6 | 12/05/2017 7 | 06/04/2017 8 | 04/04/2017
现在我打印Date1 = 01/04/2017和Date1 = 01/05 / 2017之间的所有行,
Week | nb_meeting_by_week -----+-------------------- w1 | 3 w2 | 0 w3 | 2 w4 | 3
有什么想法吗?
我尝试的是,
Select
TRUNC((TO_DATE(m.meeting_date, 'YYYYMMDD') - TO_DATE('01/04/17', 'dd-MM-yy'))/7 +1)
as week,
count(*) as nb_meeting_by_week
from meeting m
Where TO_DATE(m.meeting_date, 'YYYYMMDD') between '01/04/17' and '01/05/17'
Group by
TRUNC((TO_DATE(m.meeting_date, 'YYYYMMDD') - TO_DATE('01/04/17', 'dd-MM-yy'))/7 +1);
但我没有得到第二周[w2 |因为我本周没有开会。但是我在没有会议的所有星期都会有0个。
答案 0 :(得分:0)
您可以这样做:
Select to_char(meeting_date, 'w'), count(*)
From your_table
Where date1 = your_date and date2 = your_date2
Group by to_char(meeting_date, 'w');
这是日期功能的使用。日期上有很多功能。
那个人会给你参数日期当月的周数。
答案 1 :(得分:0)
您必须首先生成所有星期,因为它们不在您的数据库中(即表中没有每周的记录)。这是通过递归查询完成的。然后外部加入你的桌子。
with weeks(weekno, from_date, max_date) as
(
select 1 as weekno, date '2017-04-01' as from_date, date '2017-05-01' as max_date
from dual
union all
select weekno + 1 as weekno, from_date + 7 as from_date, max_date
from weeks
where from_date + 7 <= max_date
)
select weeks.weekno, count(mbw.meeting_date)
from weeks
left outer join nb_meeting_by_week mbw
on mbw.meeting_date >= weeks.from_date
and mbw.meeting_date < weeks.from_date + 7
and mbw.meeting_date <= weeks.max_date
group by weeks.weekno
order by weeks.weekno;