这个例子将有所帮助:
create table d1
( event_date date, event_description varchar2(100));
insert into d1 values (sysdate,'Phone Call');
insert into d1 values (sysdate,'Letter');
insert into d1 values (sysdate-50,'Interview');
insert into d1 values (sysdate-50,'Dinner with parents');
insert into d1 values (sysdate-100,'Birthday');
insert into d1 values (sysdate-100,'Holiday');
insert into d1 values (sysdate-100,'Interview');
insert into d1 values (sysdate-100,'Phone Call');
commit;
select * from d1;
EVENT_DATE EVENT_DESCRIPTION
------------------------- -----------------------------------------------
04-MAR-10 14.47.58 Phone Call
04-MAR-10 14.47.58 Letter
13-JAN-10 14.47.58 Interview
13-JAN-10 14.47.58 Dinner with parents
24-NOV-09 14.47.58 Birthday
24-NOV-09 14.47.58 Holiday
24-NOV-09 14.47.58 Interview
24-NOV-09 14.47.58 Phone Call
8 rows selected
您可以看到 Nov-09 是唯一一个超过 3 个事件的月份。
回到您最初的问题,即And return if for 一个月,该计数总和超过 3。以下 SQL 聚合将起作用。
select trunc(event_date,'MONTH'),count('x') from d1
having count('x') > 3 group by trunc(event_date,'MONTH')
或者,使用 to_char 将 Date 类型转换为带有 MON-YYYY 图片的 Char,如下所示:
select to_char(trunc(event_date,'MONTH'),'MON-YYYY') month,
count('x') no_of_occurances from d1 having count('x') > 3 group trunc(event_date,'MONTH')