2012-11-10 27 views
1

我需要組由多個列,但在特殊情況下:集團一列,並選擇多列SQL查詢

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

+0

是否使用'ODBC'進行函數調用? –

+2

你的問題不完全清楚,我沒有看到你當前的語法有什麼問題。你可以發佈一些樣本數據,然後得到期望的結果嗎? – Taryn

回答

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]