8

我有一个包含一组列的表,其中一个是日期列。

我需要计算该列值在同一月份出现的次数。并返回如果一个月,该计数总和超过 3。

例如:

____________________
| DATE   |  ....    |
---------------------
1998-09-02
1998-09-03
1998-10-03
1998-10-04

这必须不返回任何值。因为它没有必要的重复次数。

但这确实:

____________________
| DATE   |  ....    |
---------------------
1998-09-02
1998-09-03
1998-09-12
1998-09-14
1998-10-02
1998-11-21

为 11 月。

适用于 Oracle 数据库。

4

9 回答 9

7
SELECT
 COUNT(date)
 , TRUNC(DATE,'MON')
FROM TABLE
GROUP BY TRUNC(DATE,'MON')
HAVING COUNT(DATE) > 3
于 2010-03-03T23:27:38.593 回答
3
create table x (date_col date);

insert into x values (date '1998-09-02');
insert into x values (date '1998-09-03');
insert into x values (date '1998-09-12');
insert into x values (date '1998-09-14');
insert into x values (date '1998-10-02');
insert into x values (date '1998-11-21');

SELECT TRUNC(date_col,'MM'), count(*)
FROM x
GROUP BY TRUNC(date_col,'MM')
HAVING count(*) > 3;
于 2010-03-04T02:09:59.040 回答
1

这个例子将有所帮助:

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') 
于 2010-03-04T14:59:43.703 回答
1

因此,如果 3 列包含 1999-01-xx 你想要获取它吗?

SELECT YEAR(date), MONTH(date) 
FROM table GROUP BY YEAR(date), MONTH(date) 
HAVING COUNT(*) > 3

如果您需要包含上部结果的所有行,它应该看起来像这样

SELECT * FROM table 
INNER JOIN (
   SELECT YEAR(date) as y, MONTH(date) as m 
   FROM table GROUP BY YEAR(date), MONTH(date) 
   HAVING COUNT(*) > 3
) as virtualTable 
ON virtualTable.y = YEAR(date) AND virtualTable.m = MONTH(date)
于 2010-03-03T23:32:13.687 回答
1

理想情况下,您应该创建一个接受您需要的两个条件的存储过程,Month(integer) 和 limit(integer)

在执行以下操作的参数化过程中

 SELECT MONTH(Date) AS TheMonth, COUNT(MONTH(Date)) AS TheMonthCount
    FROM MyTable
    GROUP BY MONTH(Date)
    HAVING (COUNT(MONTH(Date)) > @limit) AND (MONTH(Date) = @month)

要同时输出相关月份,您可以使用以下命令

SELECT CAST(YEAR(Date) AS NVARCHAR) + '.' + 
       CAST(MONTH(Date) AS NVARCHAR) AS 'The  ', 
MONTH(Date ) AS TheMonth, COUNT(MONTH(Date)) AS TheMonthCount
    FROM Audit_Entry
    GROUP BY MONTH(Date), 
         CAST(YEAR(Date) AS NVARCHAR) + '.' +            
         CAST(MONTH(Date) AS NVARCHAR)
    HAVING (COUNT(MONTH(Date)) > @limit) AND (MONTH(Date) = @month)
于 2010-03-03T23:39:08.180 回答
0

这应该适用于 mysql 和 mssql:

SELECT MONTH(date), Sum(MONTH(date))
FROM table
GROUP BY date
HAVING Sum(MONTH(date)) > 3
于 2010-03-03T23:26:46.580 回答
0

我不确定您使用的是哪个数据库。

在 MySQL 中查询将类似于提出的方法@THEn

在 SQL 服务器上,您还有其他有趣的可能性。

阅读本文了解更多详情。

于 2010-03-03T23:38:59.163 回答
0

您可以使用 Oracle 的 EXTRACT 方法:

select theMonth, sum(monthCount)
from (
  select 
    extract(MONTH FROM t.theDateColumn) as theMonth,
    1 as monthCount
  )
group by theMonth
having sum(monthCount) >= 3

目前我手头没有 Oracle 数据库,因此此代码可能无法按原样运行 - 我对此表示歉意。

于 2010-03-04T00:01:36.530 回答
-2

可能是错的,但猜测:

SELECT SUM(date) FROM table
GROUP BY date where SUM(date) > 3
于 2010-03-03T23:21:34.190 回答