Reputation: 21
I'm using SQL Server and I'm trying to write a query that returns the total distance driven to meetings in 2015, 2016 etc. and then grouping it by year.
What I've got:
SELECT
DATEPART(yyyy, MeetupDate) AS Year,
SUM(Distance) AS Distance
FROM
Meetups
GROUP BY
MeetupDate
Returns this where MeetupDate
's data type is date
and Distance
data type is decimal(5, 2)
But what I'd like it to do instead is list it as:
2015 - NULL
2016 - 97.8 (total)
How can I achieve this?
Upvotes: 2
Views: 912
Reputation: 306
SELECT
DATEPART(yyyy, MeetupDate) Year
,SUM(Distance) Distance
FROM
Meetups
GROUP BY
DATEPART(yyyy, MeetupDate)
Upvotes: 4