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

大虾门帮忙大型数据库SQL语句问题
SQL数据库中有一表TDATA,有字段AA,BB,CC,DD,EE,FF,GG,T100,T1000,T10000
表中的记录有近1000万条,字段AA,BB,CC,DD,EE,FF,GG里面存储的是整型数字,从0-10000的数字,现在要做的是:
每行记录从AA,BB,CC,DD,EE,FF,GG这几个字段里面判断小于100,小于1000,小于10000的数字各有几个,并把小于100的数字个数存放在T100,小于1000但大于100的数字个数存放在T1000,小于10000并大于1000的数字个数存放在T10000里面,用SQL语句如何做到呢?
例如:
ID         AA         BB             CC           DD         EE             FF         GG       T100       T1000       T10000
1           56         700         800         6000       3000       79         90      
那么通过SQL来实现将T100里面的值是3,T1000里面的值是2,T10000里面的值是2
如何通过SQL来实现?



------解决方案--------------------
update TDATA
set T100 = case when AA <100 then 1 else 0 end
+ case when BB <100 then 1 else 0 end
+ case when CC <100 then 1 else 0 end
+ case when DD <100 then 1 else 0 end
+ case when EE <100 then 1 else 0 end
+ case when FF <100 then 1 else 0 end
+ case when GG <100 then 1 else 0 end,
T1000 = case when AA <1000 and AA> 100 then 1 else 0 end
+ case when BB <1000 and BB> 100 then 1 else 0 end
+ case when CC <1000 and CC> 100 then 1 else 0 end
+ case when DD <1000 and DD> 100 then 1 else 0 end
+ case when EE <1000 and EE> 100 then 1 else 0 end
+ case when FF <1000 and FF> 100 then 1 else 0 end
+ case when GG <1000 and GG> 100 then 1 else 0 end,
T10000 = case when AA> 1000 then 1 else 0 end
+ case when BB> 1000 then 1 else 0 end
+ case when CC> 1000 then 1 else 0 end
+ case when DD> 1000 then 1 else 0 end
+ case when EE> 1000 then 1 else 0 end
+ case when FF> 1000 then 1 else 0 end
+ case when GG> 1000 then 1 else 0 end