SQL Group by Year

前端 未结 6 954
别那么骄傲
别那么骄傲 2021-01-07 21:09

This is my query.

select CONVERT(varchar, cast(date as datetime), 3)
from shoptransfer 
group by year (date)

I want to group by the year pa

6条回答
  •  难免孤独
    2021-01-07 21:50

    If you want to select both the date and the year you should not use a GROUP BY clause as it combines all of the rows with similar years into one row. If you want all of the dates with similar years together you can use an ORDER BY:

    SELECT DATEPART('yyyy',date) AS Year,date
    FROM shoptransfer 
    

    OR

    SELECT DATEPART('yyyy',date) AS Year,date
    FROM shoptransfer
    ORDER BY DATEPART('yyyy',date) desc
    

提交回复
热议问题