求一个sql,急,在线等.

tian00weiwei 2005-07-15 03:52:42
t表里有一个字段D,是记录发生时间戳。字段F,是状态(N,Y)。
t(a varchar2,
b varchar2,
d date, --记录发生时间戳
f varchar2 --是状态(N,Y))
a和b联合起来唯一表示一个用户。
我怎么能取出,在今天这个表里出现的用户,就在今天(24小时内)先出现了状态为Y的记录,又出现了状态为N的记录?
...全文
122 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tian00weiwei 2005-07-15
  • 打赏
  • 举报
回复
对,广丽的思路对。谢谢大家.我出去加点分,给大家分。分不多,见谅.
再求;一SQL.我另起一贴
njhart2003 2005-07-15
  • 打赏
  • 举报
回复
不知道,楼主是要找出当天状态发生变化的用户?还是要找出具体发生变化的时刻?
njhart2003 2005-07-15
  • 打赏
  • 举报
回复
广丽的思路对!

觉得要做些修改,
如果今天的记录有100条,前99条一直是Y,最后一条是N,那么这么查出来的就有99条,而实际应该是查出有Y-->N的记录。应该是99+100那两条记录,对不?

小李木耳 2005-07-15
  • 打赏
  • 举报
回复
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
guangli_zhang 2005-07-15
  • 打赏
  • 举报
回复
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))
guangli_zhang 2005-07-15
  • 打赏
  • 举报
回复
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')
njhart2003 2005-07-15
  • 打赏
  • 举报
回复
楼主,是不是找出今天,状态先为Y后为N的用户记录?

这样行不行:


select Y.a,Y.b,Y.max_d,N.min_d
from
(select a,b,max(d) max_d
from t
where d>trunc(sysdate)
and f='Y'
group by a,b) Y,

(select a,b,min(d) min_d
from t
where d>trunc(sysdate)
and f='N'
group by a,b) N

where Y.max_d<=N.min_d
and Y.a=N.a
and Y.b=N.b;


njhart2003 2005-07-15
  • 打赏
  • 举报
回复
楼主,是不是找出今天,状态先为Y后为N的用户记录?

这样行不行:


select Y.a,Y.b,Y.max_d,N.min_d
from
(select a,b,max(d) max_d
from t
where d>trunc(sysdate)
and f='Y'
group by a,b) Y,

(select a,b,min(d) min_d
from t
where d>trunc(sysdate)
and f='N'
group by a,b) N

where Y.max_d<=N.min_d
and Y.a=N.a
and Y.b=N.b;


tian00weiwei 2005-07-15
  • 打赏
  • 举报
回复
24小时内可能有多条记录.每一个动作,都会产生一条记录,状态或者为Y,或者为N,是多条记录。
bzszp 2005-07-15
  • 打赏
  • 举报
回复
select a,b from t
where d>trunc(sysdate) group by a,b having count(*)>1;
zjw7789 2005-07-15
  • 打赏
  • 举报
回复
楼主,看你的意思好像是要获取一个记录的修改时间再和记录发生时间做比较吗?
lee_billiy 2005-07-15
  • 打赏
  • 举报
回复
什么意思?你要先判断这字段先为Y后变成N?SQL语句只能判断数据库当前的数值,不能识别一个过程的变化吧,楼主如果想要看到这种变化最好建2个字段来识别!

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧