Ramakrishnan Ramar
Ramakrishnan Ramar

Reputation: 27

SQL Group by Year based on after and Before,

I have set of data Name, Id and Joined Date ,want to Group by Year 2019 before joined members and after joined members, need those counts. I tried below,

Select Year(JoinedDate),Count(1) from UserDetails
Group by Year(JoinedDate);

It's listed all the year, Please help me to get the solution. Actual Data: enter image description here

Expected Output:

enter image description here

Upvotes: 0

Views: 47

Answers (1)

Squirrel
Squirrel

Reputation: 24793

Looks to me you want a conditional sum

SELECT CASE WHEN JoinedDate < '20190101' THEN '2019 before' else '2019 after' END AS [Date], 
       count(*) AS [Count]
FROM   UserDetails
GROUP BY CASE WHEN JoinedDate < '20190101' THEN '2019 before' else '2019 after' END

Upvotes: 1

Related Questions