t表里有一个字段D,是记录发生时间戳。字段F,是状态(N,Y)。
t(a varchar2,
b varchar2,
d date, --记录发生时间戳
f varchar2 --是状态(N,Y))
a和b联合起来唯一表示一个用户。
我怎么能取出,在今天这个表里出现的用户,就在今天(24小时内)先出现了状态为Y的记录,又出现了状态为N的记录?
...全文
12212打赏收藏
求一个sql,急,在线等.
t表里有一个字段D,是记录发生时间戳。字段F,是状态(N,Y)。 t(a varchar2, b varchar2, d date, --记录发生时间戳 f varchar2 --是状态(N,Y)) a和b联合起来唯一表示一个用户。 我怎么能取出,在今天这个表里出现的用户,就在今天(24小时内)先出现了状态为Y的记录,又出现了状态为N的记录?
select WW.USERNAME from
(select a||b AS USERNAME from t
where t=to_char(sysdate,'yyyymmdd')
and f='N' ) WW,
(select a||b AS USERNAME from t
where t=to_char(sysdate,'yyyymmdd')
and f='Y' ) XX
WHERE WW.USERNAME=XX.USERNAME
select a,b from t t1
where f ='Y'
and trunc(d) = trunc(sysdate)
and exists(select 1 from t t2 where t2.a=t1.a and t2.b=t1.b and t2.d>t1.d and t2.f='N' and trunc(t2.d) = trunc(sysdate))
select a,b from t t1
where f ='Y'
and trunc(d) = trunc(sysdate)
and exists(select 1 from t t2 where t2.a=t1.a and t2.b=t1.b and t2.d>t1.d and t2.f='N')