按月份和名称进行数据计算

时间:2019-05-14 13:25:09

标签: sql sql-server tsql

我的销售表带有以下列

Date        / Total / Person Name

2019-05-25  / 500   / george
2019-05-28  / 800   / george
2019-01-15  / 100   / mark
2019-01-16  / 600   / mark
2019-02-18  / 500   / pavel

我想让结果像下面这样

05(month)  / total 1300 / georga
1 (month)  / total 700  / mark
2 (motn)   / total      / pavel

每个月每个人都有很多销售额的总和

2 个答案:

答案 0 :(得分:1)

要按月初分组:

select  dateadd(m, datediff(m, 0, t1.date), 0) as themonth,
        sum(t1.total) as total,
        t1.name
from t1
group by t1.name, dateadd(m, datediff(m, 0, t1.date), 0)

答案 1 :(得分:0)

SELECT SUM(Total)
    ,DATEPART(MONTH, DATE)
    ,[Person Name]
FROM myTAble
GROUP BY DATEPART(MONTH, DATE)
    ,[Person Name]