I need to make a report with logged users by date range but without duplicates on the same day (if someone was logged twice on the same day we won't list it twice). unfortunately, we keep login information as json (yeah, I can't change it to separate table, I don't know who designed this db). Query to see all logged user:
select a.id, username, email, ah.modified as login_date
from accounts a join
account_history ah
on modified_acc_id = a.id
where ah.data::jsonb->>'message' = 'Logon';
Modified is timestamp with timezone and it is used as login date.
I found only examples with the count distinct ids per day but I don't know how to modified it to return distinct results per day
sample data:
id | username | email | login_date
-----+-------------------------+---------------------------------+----------------------------
102 | example | example@example.com | 2018-12-06 09:30:10.573+00
102 | example | example@example.com | 2018-12-06 09:32:34.235+00
42 | rafal | rafal@example.com | 2018-12-06 09:45:24.884+00
576 | john | john@example.com | 2018-12-06 09:35:24.922+00
576 | john | john@example.com | 2018-12-07 09:58:04.253+00
wanted data:
id | username | email | login_date
-----+-------------------------+---------------------------------+----------------------------
102 | example | example@example.com | 2018-12-06 09:30:10.573+00
42 | rafal | rafal@example.com | 2018-12-06 09:45:24.884+00
576 | john | john@example.com | 2018-12-06 09:35:24.922+00
576 | john | john@example.com | 2018-12-07 09:58:04.253+00
So as you can see, without second row