日期列的SQL计数

时间:2010-03-03 23:18:08

标签: sql oracle date-manipulation

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

我需要计算该列的值出现在同一个月的次数。如果是一个月,则返回该计数总和超过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

十一月。

适用于Oracle DB。

9 个答案:

答案 0 :(得分:7)

SELECT
 COUNT(date)
 , TRUNC(DATE,'MON')
FROM TABLE
GROUP BY TRUNC(DATE,'MON')
HAVING COUNT(DATE) > 3

答案 1 :(得分: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;

答案 2 :(得分: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)

答案 3 :(得分:1)

理想情况下,您应该创建一个存储过程,接受您需要的两个条件,月(整数)和限制(整数)

在执行以下

的参数化过程中
 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)

答案 4 :(得分: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

你可以看到11月09日是超过3个活动的唯一一个月。

回顾你原来的问题,即如果一个月又返回,则该数量总和超过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') 

答案 5 :(得分:0)

这适用于mysql和mssql:

SELECT MONTH(date), Sum(MONTH(date))
FROM table
GROUP BY date
HAVING Sum(MONTH(date)) > 3

答案 6 :(得分:0)

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

在MySQL中,查询类似于@THEn

提出的方法

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

阅读此article了解详情。

答案 7 :(得分: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数据库,所以这段代码可能无法正常工作 - 我为此道歉。

答案 8 :(得分:-2)

可能是错误但猜测:

SELECT SUM(date) FROM table
GROUP BY date where SUM(date) > 3