I’m trying to validate the users logged in based on month. SQL
current df
user id date
P1302 2023-11-01
P1302 2023-10-01
P1302 2023-09-01
P1302 2023-08-01
P1302 2023-07-01
P1302 2023-06-01
P1301 2023-11-01
P1301 2023-10-01
P1301 2023-08-01
P1301 2023-07-01
P1301 2023-06-01
expected result
user id Jun Jul aug sep oct nov
1302 1 1 1 1 1 1
1301 1 1 1 0 1 1
4
Answers
You can do it with conditional aggregation:
One common approach to your problem is to use count over a case expression:
You can use the
SUM
function with conditional statements(MONTH(date) = month_number)
to count the occurrences of logins for each user in each month.See this db<>fiddle.