按列分组并选择多个列的Sql查询

时间:2012-11-10 15:37:12

标签: sql sql-server

我需要按多列分组,但在特殊情况下:

我有一张(Payment_Type,Year,TotalMoney)

的表格

我需要仅按payment_type(cash or credit)获得总分组总和,我需要在查询中选择年份,我该怎么做?我的疑问是:

select Payment_Type,Year,SUM(TotalMoney) 
from table 
where 1=1 
group by Payment_Type,Year

我收到错误消息:

  

年份不包含在聚合函数或GROUP BY子句

1 个答案:

答案 0 :(得分:2)

select Payment_Type,Year(YourDateColumn),SUM(TotalMoney) 
from table 
group by Payment_Type,Year(YourDateColumn)

如果你的专栏命名为年,那么

select Payment_Type,[Year],SUM(TotalMoney) 
from table 
group by Payment_Type,[Year]
相关问题