我现在有一个表,
hid pk, hacc 户号 , htype 分为0 1 两种情况 , fCode 基金代码 ,
share 成交量(number) ,tdate 成交时间 我用一条sql语句实现以下效果select (count(b.hacc) / count(a.hacc)) reta ,( count(c.hacc) /count(a.hacc)) retb
from table a, (select * from table where htype = 0) b, (select * from table where htype = 1) 根据fcode查例:hid hacc htype fcode share tdate
1 001 0 0001 20 20080101
2 002 0 0001 10 20080101
3 002 1 0001 50 20080101
4 002 1 0002 60 20080101
结果:0001的 reta=2/3=0.666 retb=1/3=0.333
hid pk, hacc 户号 , htype 分为0 1 两种情况 , fCode 基金代码 ,
share 成交量(number) ,tdate 成交时间 我用一条sql语句实现以下效果select (count(b.hacc) / count(a.hacc)) reta ,( count(c.hacc) /count(a.hacc)) retb
from table a, (select * from table where htype = 0) b, (select * from table where htype = 1) 根据fcode查例:hid hacc htype fcode share tdate
1 001 0 0001 20 20080101
2 002 0 0001 10 20080101
3 002 1 0001 50 20080101
4 002 1 0002 60 20080101
结果:0001的 reta=2/3=0.666 retb=1/3=0.333
select (count(case when b.hacc=0 then 1 else null end ) / count(a.hacc)) reta ,
( count(case when b.hacc=1 then 1 else null end ) /count(a.hacc)) retb
from table a, table case when then .. else end 或者decode
case when then .. else end 或者decode
是什么啊,以及他的用法,这个有点看不懂
1 001 0 0001 20 20080101
2 002 0 0001 10 20080101
3 002 1 0001 50 20080101
4 002 1 0002 60 20080101 retb= ( (hid=3,1条)/hid=1,2,3)=0.333
reta= ( (hid=1,2,1条)/hid=1,2,3)=0.666
case when ... 就相当于 if else 的语法
decode也是的,decode(字段,预值1,结果1,预值2,结果2,都不符合的值)