简体   繁体   中英

SQL Query Join plus sum and months sorting

i have a problem using the sum function with the inner join in the below query

SELECT distinct Datename(Month,(date)), year(date), SUM(tblpayment.Amount)  
FROM TblSubsc INNER JOIN TBLPayment ON 
TblSubsc.[Subsc ID]=TBLPayment.[Subsc ID] 
group by year(date),Datename(Month,(date))

it is giving the sum but the months are not sorted so i get

April
August
February
January
March
May

please any help would be appreciated

Try to add the order by clause

order by year(date) asc, month(date) asc

and add

month(date)

to group by too.

EDIT: remove "distinct" as it's not necessary in this case because the data is already grouped by month & date.

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