如何每月计算数字,然后在同一选择语句中取其平均值

时间:2019-06-21 21:35:31

标签: sql-server tsql sql-server-2012 average

在以下情况下,如何计算每月的总平均值?:

我们有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

![enter image description here

1 个答案:

答案 0 :(得分:2)

也许是这样的

示例

min-y

返回

enter image description here