sql >> Databasteknik >  >> RDS >> Sqlserver

Hur grupperar jag efter månad med SQL Server?

SELECT CONVERT(NVARCHAR(10), PaymentDate, 120) [Month], SUM(Amount) [TotalAmount]
FROM Payments
GROUP BY CONVERT(NVARCHAR(10), PaymentDate, 120)
ORDER BY [Month]

Du kan också prova:

SELECT DATEPART(Year, PaymentDate) Year, DATEPART(Month, PaymentDate) Month, SUM(Amount) [TotalAmount]
FROM Payments
GROUP BY DATEPART(Year, PaymentDate), DATEPART(Month, PaymentDate)
ORDER BY Year, Month


  1. Konfigurera Master-Master MySQL-databasreplikering

  2. ODP.net-hanterad drivrutin kastar ORA-12570:Nätverkssession:Oväntat paketläsfel

  3. NodeJS MySQL Dump

  4. Vad är utökad lagrad procedur i MS SQL Server