【问题标题】:Group by one column and select more than one column Sql query按一列分组并选择多列Sql查询
【发布时间】:2012-11-10 15:37:11
【问题描述】:

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

我有一张(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 子句中

【问题讨论】:

  • 你在使用ODBC进行函数调用吗?
  • 您的问题并不完全清楚,我认为您当前的语法没有任何问题。您可以发布一些示例数据,然后发布所需的结果吗?

标签: sql sql-server


【解决方案1】:
select Payment_Type,Year(YourDateColumn),SUM(TotalMoney) 
from table 
group by Payment_Type,Year(YourDateColumn)

如果您的列名为 year 则

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2021-04-15
    • 2022-10-09
    • 2019-04-08
    相关资源
    最近更新 更多