日期:2014-05-17  浏览次数:20700 次

【求教】查询条件下同字段值的另一条件记录
SQL code

/*
--TB1表数据示例:
CusID    IDtimes    Atime    Btime    Ctime    Dtype
A-A    1    0    0    23    3
A-A    2    30    0    0    1
A-A    -1    10    1    120    2
B-B    1    0    0    11    3
B-B    -1    50    0    0    1
--...
*/

DESC TB1;
/*
CusID            NOT NULL VARCHAR2(25)                
IDtimes            NUMBER(4)  
Atime/Btime/Ctime    DATE 
Dtype            NUMBER(4)
*/

--求Dtype=2且(Btime+Ctime)>0记录同CusID的、满足的Dtype=1且IDtimes=2的记录行!
--求教,谢谢方家~
--------<2010.12.21 afternoon>



------解决方案--------------------
SQL code
select count(*) from TB1 t
where t.Dtype=1 and t.IDtimes=2 and 
      exists(select null from TB1 t1 
             where t.CusID=t1.CusID and t1.Dtype=2 and (t1.Btime+t1.Ctime)>0)

------解决方案--------------------
SQL code

--其实这句话没怎么明白:
--求Dtype=2且(Btime+Ctime)>0记录同CusID的、满足的Dtype=1且IDtimes=2的记录行!
select count(*) cnt from TB1 a
where a.Dtype=2 and (a.Btime+a.Ctime)>0 
and exists(select 1 from TB1 b where a.CusID=b.CusID and b.Dtype=1 and b.IDtimes=2 )

------解决方案--------------------
探讨
SQL code

/*
--TB1表数据示例:
CusID IDtimes Atime Btime Ctime Dtype
A-A 1 0 0 23 3
A-A 2 30 0 0 1
A-A -1 10 1 120 2
B-B 1 0 0 ……