如何在SQL中按日期范围分组

问题描述

|| 从我发现有用的StackOverflow.com的另一篇帖子中借鉴。 我想知道是否可以使用我未明确定义的日期范围进行分组。 为了澄清起见,假设我有一个名为Person的表和一个名为Date Of Birth的列。 我想分组计算一个时期内出生的人数。 所以看起来像这样。 天生伯爵 1980年3月25日 1980年4月452 1980年5月42 ... 1993年12月452 期间可能是几周,几个月和一季度。 如果使用其他解决方案,则需要指定每个日期范围,即使是数月或数年,由于人们的出生日期相差很大,因此还需要很长时间。
select t.dob as [dob],count(*) as [number of occurences]
from (
      select user_id,case when date >= \'1980-01-01\' and date < \'1980-02-01 then \'Jan 1980\'
         when date >= \'1980-02-01\' and date < \'1980-03-01 then \'Feb 1980\'
         ...
         when date >= \'1990-03-01\' and date < \'1990-04-01 then \'Mar 1990\'
        else \'Null\' end as dob
     from Person) t
group by t.dob
您能想到什么解决方案,可以让我仅指定我希望数据范围为间隔的大小,并按这些日期范围进行分组吗?     

解决方法

        这样的地址可以解决您的需求吗?诀窍是使用和滥用CONVERT函数来使您的时间段很好地组合在一起,然后使用DENSE_RANK将其减少为单调递增的序列。
WITH DUMMY_DATA AS
(
-- use a convert with a type of 112 to coerce dates to
-- YYYY-MM format and truncate days
-- Be creative,use to generate whatever bands you desire
SELECT CONVERT(char(7),\'2011-06-01\',112) AS period,\'stuff\' AS junk
UNION ALL SELECT CONVERT(char(7),\'2011-05-01\',\'more stuf\' AS junk
UNION ALL SELECT CONVERT(char(7),\'more stuf1\' AS junk
UNION ALL SELECT CONVERT(char(7),\'more stuf2\' AS junk
UNION ALL SELECT CONVERT(char(7),\'more stuf3\' AS junk
UNION ALL SELECT CONVERT(char(7),\'more stu4f\' AS junk
UNION ALL SELECT CONVERT(char(7),\'more stuf5\' AS junk
UNION ALL SELECT CONVERT(char(7),\'more stuf7\' AS junk
UNION ALL SELECT CONVERT(char(7),\'more stuf8\' AS junk
UNION ALL SELECT CONVERT(char(7),\'more stuf9\' AS junk
UNION ALL SELECT CONVERT(char(7),\'more stuf0\' AS junk
),BANDS AS
(
-- Using our data,create bands in case you want to look
-- at groupings 
SELECT
    D.*,DENSE_RANK() OVER (ORDER BY D.period ASC) AS banding
FROM
    DUMMY_DATA D
),ROLLUPS AS
(
-- ROll up based on our banding
SELECT
    B.period,B.banding,COUNT(1) AS row_count
FROM
    BANDS B
GROUP BY
    B.period,B.banding
) 选择*从ROLLUPS R 使用上面的,我得到的输出像
[period]   [band]  [row_count]
2011-05    1       10
2011-06    2       1
    ,        您可能会使用类似的东西并利用DATEPART函数 请。不要忘记取消注释您要测试的语句。
WITH DummyTable AS (
SELECT \'05/01/2011\' AS DateOfBirth,\'Peter\' AS CustomerName UNION ALL
SELECT \'06/02/2011\' AS DateOfBirth,\'Bill\' AS CustomerName UNION ALL
SELECT \'05/01/2011\' AS DateOfBirth,\'Charles\' AS CustomerName UNION ALL
SELECT \'07/13/2010\' AS DateOfBirth,\'Maria\' AS CustomerName UNION ALL
SELECT \'01/01/2009\' AS DateOfBirth,\'Theresa\' AS CustomerName UNION ALL
SELECT \'05/01/2011\' AS DateOfBirth,\'Steven\' AS CustomerName UNION ALL
SELECT \'06/02/2011\' AS DateOfBirth,\'Matthew\' AS CustomerName UNION ALL
SELECT \'07/13/2010\' AS DateOfBirth,\'Rachel\' AS CustomerName UNION ALL
SELECT \'05/01/2011\' AS DateOfBirth,\'Molly\' AS CustomerName UNION ALL
SELECT \'07/13/2010\' AS DateOfBirth,\'Alex\' AS CustomerName UNION ALL
SELECT \'08/01/2009\' AS DateOfBirth,\'John\' AS CustomerName UNION ALL
SELECT \'07/13/2010\' AS DateOfBirth,\'Ann\' AS CustomerName UNION ALL
SELECT \'05/01/2011\' AS DateOfBirth,\'Jay\' AS CustomerName
) 
--By Month

--SELECT DATENAME(month,DateOfBirth)+DATENAME(year,dateofBirth),COUNT(*)
--FROM DummyTable
--GROUP BY DATENAME(month,dateofBirth)


--BY Quarter

--SELECT CAST(DATEPART(QUARTER,DateOfBirth) AS Varchar)+\' quarter \'+DATENAME(year,COUNT(*)
--FROM DummyTable
--GROUP BY CAST(DATEPART(QUARTER,dateofBirth)