首页 新闻 会员 周边 捐助

这样的子查询应该怎么避免

0
悬赏园豆:20 [已解决问题] 解决于 2012-07-09 17:29
    select Spreader,counts,
        (select COUNT(*) from view_Login 
            where [loginNum]>0 and Spreader=v.Spreader) as one
            ,(select COUNT(*) from view_Login 
            where [loginNum]>1 and Spreader=v.Spreader) as two
            ,(select COUNT(*) from view_Login 
            where [loginNum]>2 and Spreader=v.Spreader) as three
            from view_Login as v
            group  by Spreader,counts
view_Login  是推广登录的日志表
Spreader 是推广员
counts 是推广人数
uid 是用户ID
loginNum 是登录数

现在要统计推广用户登录的情况,最后写出这样一个粗糙的SQL语句,应该还有更好的写法,特求
後生哥哥的主页 後生哥哥 | 初学一级 | 园豆:23
提问于:2012-07-09 16:16
< >
分享
最佳答案
0

使用case  when 转换

select 

Spreader,counts
,SUM(case when
[loginNum]>0 then 1 else 0 end) one
,SUM(case when  [loginNum]>1   then 1 else 0 end )  two
,SUM(case when  [loginNum]>2   then 1 else 0 end ) three
from view_Login as v
            group  by Spreader,counts
收获园豆:20
acepro | 小虾三级 |园豆:1218 | 2012-07-09 16:55

学到了,谢谢

後生哥哥 | 园豆:23 (初学一级) | 2012-07-09 17:29
其他回答(1)
0

顶!!!!!!!!!!!

Alec-Yin | 园豆:188 (初学一级) | 2012-07-09 16:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册