在以下情况下,如何计算每月的总平均值?:
我们有9个ClaimID。因此Aveage将是9/6个独立月= 1.5
DECLARE @TestTable TABLE (claimid int, DateClosed datetime)
INSERT INTO @TestTable
VALUES (111, '01-01-2018'), (222, '01-03-2018'), (333, '01-12-2018'),
(444, '07-03-2018'), (555, '08-15-2018'), (666, '09-13-2018'),
(777, '04-03-2019'), (888, '05-01-2019'), (999, '07-01-2018'),
(1000, NULL), (1100, NULL), (1200, NULL)
SELECT
ClaimID,
CAST(DateClosed AS DATE) AS DateClosed,
COUNT(ClaimID) CountClaimID,
COUNT(claimid) OVER (PARTITION BY MONT(DateClosed), YEAR(DateClosed)) AS CountPerMonth
FROM
@TestTable
GROUP BY
ClaimID, DateClosed