Sql Query:按月分组&本案年份

时间:2013-04-03 18:12:05

标签: sql-server sql-server-2008 tsql sql-server-2008-r2

我有一个表产品,其中包含以下列

ProductId Name RegistrationDate UnregistrationDate
1          AB    2013-01-01      2013-03-01
2          CD    2013-01-10      2013-03-13

我想获得一年中每个月的注册产品列表。

示例:年,月和已注册但未注册的经销商数量。

Year Month RegisteredProucts
2013  2         35
2013  3         45(includes products even registered before March 2013)

我编写了以下存储过程来查找已注册的产品一个月: &安培;它有效:

@Begin Time = First Day of the Month
@End Time = Last Day of the Month


select COUNT(DISTINCT P.ProductId) as RegisteredProducts from Product P    
where ((P.RegisteredDate < @EndTime)
AND (P.UnregisteredDate > @EndTime))  

然后我写了下面的查询,但它似乎是通过RegisteredDate对结果进行分组。 我想知道如何在每月月底之前对注册产品(未注册的产品)进行分组 为期一年?

select YEAR(P.RegisteredDate) AS [YEAR],MONTH(P.RegisteredDate) AS [MONTH],       COUNT(DISTINCT P.ProductId) as RegisteredProducts from Product P    
where ((P.RegisteredDate < @EndTime)
AND (P.UnregisteredDate > @EndTime))  
group by YEAR(D.RegisteredDate), MONTH(D.RegisteredDate)

2 个答案:

答案 0 :(得分:1)

WITH    months (mon) AS
        (
        SELECT  CAST('2013-01-01' AS DATE) AS mon
        UNION ALL
        SELECT  DATEADD(month, 1, mon)
        FROM    months
        WHERE   mon < DATEADD(month, -1, GETDATE())
        )
SELECT  mon, COUNT(productId)
FROM    months
LEFT JOIN
        registeredProducts
ON      registrationDate < DATEADD(month, 1, mon)
        AND (unregistrationDate >= mon OR unregistrationDate IS NULL)
GROUP BY
        mon

答案 1 :(得分:1)

; with  months as
        (
        select  cast('2013-01-01' as date) as dt
        union all
        select  dateadd(month, 1, dt)
        from    months
        where   dt < '2014-01-01'
        )
select  *
from    months m
cross apply
        (
        select  count(*) as ProductCount
        from    Product p
        where   p.RegistrationDate < dateadd(month, 1, m.dt) and 
                (
                    UnregistrationDate is null
                    or UnregistrationDate >= m.dt
                )
        ) p

Example at SQL Fiddle.