So, I am trying to count my users who login 7,15, & 30 days ago from sysdate. I know I can count users who loged in 7 days ago and I can group them, but I would like to count users who loged in 7, 15, & 30 days ago.
select roel, count(column_name)
FROM USER , STRUCTURE
where login_time > sysdate - 7
group by role
I would like to get the results below:
Role 7days ago 15 days ago 30 days ago Never
Admin 1 20 0 150
If you want to count users, the basic idea is conditional aggregation. Assuming that login_time
and sysdate
are both in the user
table, you can do:
select role,
sum(case when login_time > sysdate - 7 then 1 else 0 end) as count_07,
sum(case when login_time > sysdate - 15 then 1 else 0 end) as count_15,
sum(case when login_time > sysdate - 30 then 1 else 0 end) as count_30
from user u
group by role;
Why you are doing a Cartesian product between user
and structure
is unclear. Perhaps a join is actually needed for the data you have.
Also, it is unusual to use sysdate
for this purpose, because sysdate
has a time component. Normally, you just want to look at the dates.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.