日期:2014-05-18  浏览次数:20487 次

求一個好的算法(至少兩個字段不為空)
現在有一張表TT(Tid,Idate,T1,T2,T3,T4,T5,T6),有幾百萬條數據,現在要求查詢出字段T1,T2,T3,T4,T5,T6中至少有兩個字段不為空的數據。
請教有什麼好的算法可能實現!!
先謝謝!!

------解决方案--------------------
現在有一張表TT(Tid,Idate,T1,T2,T3,T4,T5,T6),有幾百萬條數據,現在要求查詢出字段T1,T2,T3,T4,T5,T6中至少有兩個字段不為空的數據。
請教有什麼好的算法可能實現!!
先謝謝!!

--笨办法
select * from tt where t1 is not null and t2 is not null
union all
select * from tt where t1 is not null and t3 is not null
union all
select * from tt where t1 is not null and t4 is not null
union all
select * from tt where t1 is not null and t5 is not null
union all
select * from tt where t1 is not null and t6 is not null
union all
select * from tt where t2 is not null and t3 is not null
union all
select * from tt where t2 is not null and t4 is not null
union all
select * from tt where t2 is not null and t5 is not null
union all
select * from tt where t2 is not null and t6 is not null
union all
select * from tt where t3 is not null and t4 is not null
union all
select * from tt where t3 is not null and t5 is not null
union all
select * from tt where t3 is not null and t6 is not null
union all
select * from tt where t4 is not null and t5 is not null
union all
select * from tt where t4 is not null and t6 is not null
union all
select * from tt where t5 is not null and t6 is not null

------解决方案--------------------
select * from tt where
decode(t1,null,0,1)+decode(t2,null,0,1)+decode(t3,null,0,1)+
decode(t4,null,0,1)+decode(t5,null,0,1)+decode(t6,null,0,1)> =2
------解决方案--------------------
乌龟的这写法可以,顶一下`
select * , nullnum = case t1 is not null then 1 else 0 end +
case t2 is not null then 1 else 0 end +
case t3 is not null then 1 else 0 end +
case t4 is not null then 1 else 0 end +
case t5 is not null then 1 else 0 end +
case t6 is not null then 1 else 0 end
from tt
where nullnum > = 2