简体   繁体   中英

Group by one column and select more than one column Sql query

I need to group by more than one columns but in special case:

I have a table of (Payment_Type,Year,TotalMoney)

I need to get sum of total grouping by payment_type(cash or credit) only and I need to select year in my query how can I do it? my query is:

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

I get an error message as:

Year is not contained in either an aggregate function or the GROUP BY clause

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

if your column is named year then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM