1

我有一个包含以下列的表格产品

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

ETC

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

示例:年、月以及已注册和未未注册的经销商数量。

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)
4

2 回答 2

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
于 2013-04-03T18:21:27.917 回答
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

SQL Fiddle 的示例。

于 2013-04-03T18:25:37.980 回答