用于简单查询的SQL循环

时间:2016-03-11 23:12:56

标签: sql loops

我是SQL的新手,需要循环查询20次并输入摘要表输出。最有效的方法是什么?

From lower = 1 to lower = 20

select distinct middle, sum(case when signups>0 then 1 else 0 end) signup_uv 
from HULU.dbo.Jan_core_converter where  lower =1 

group by middle having middle <=20 order by middle 

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我能想到这一点。这应该避免至少循环。沿着这些方向努力应该是一个很好的起点:

SELECT         distinct con.middle,
               SUM(CASE
                    WHEN con.signups > 0 THEN 1 ELSE 0 END) signup_uv 
FROM           HULU.dbo.Jan_core_converter con
WHERE          con.lower BETWEEN 1 AND 20
GROUP BY       con.middle
ORDER BY       con.middle

希望这有帮助!!!