请解决此查询
select
to_char(date_trunc('day',create_time),'DD Month YYYY') as date,
to_char(min(create_time),'HH24:mi') as login_time where login_status = 1,
to_char(max(create_time),'HH24:mi') as login_out where login_status = 0
from
users.access_log
where
user_id = 1010
group by
user_id, actionlink_id, date_trunc('day', create_time)
在“,”
处或附近收到错误不知道怎么写to_char函数中的where条件。
答案 0 :(得分:2)
您需要CASE expression,而不是WHERE:
select
to_char(date_trunc('day',create_time),'DD Month YYYY') as date,
case when login_status = 1 then to_char(min(create_time),'HH24:mi') end as login_time,
case when login_status = 0 then to_char(max(create_time),'HH24:mi') end as login_out
from
users.access_log
where
user_id = 1010
group by
user_id, actionlink_id, date_trunc('day', create_time)
答案 1 :(得分:0)
使用join
将两个登录和注销行放在一起:
select
to_char(date_trunc('day',create_time),'DD Month YYYY') as date,
to_char(min(a.create_time),'HH24:mi') as login_time,
to_char(max(b.create_time),'HH24:mi') as login_out
from users.access_log a
left join users.access_log b on b.user_id = a.user_id
and b.login_status = 0
and date_trunc('day', b.create_time) = date_trunc('day', a.create_time)
and b.actionlink_id = a.actionlink_id
where a.user_id = 1010
and a.login_status = 1
group by a.user_id, a.actionlink_id, date_trunc('day', a.create_time)