选择满足WHERE子句的分数的优雅方法

时间:2014-01-14 23:44:39

标签: sql

我有一个包含以下架构的SQL表:

{playerId,playSessionLength,timestamp}

我想在一定时间范围内找到不到10秒的比赛时间。

我知道怎么做这个丑陋,有两个问题:

select count(*) from playData where timestamp > start and timestamp < end
select count(*) from playData where timestamp > start and timestamp < end and playSessionLength < 10

然后进行B / A的划分。

我的问题是,使用单个查询执行此操作的最干净,最优雅的方法是什么?

1 个答案:

答案 0 :(得分:1)

我会尝试:

select 100*count(case when playSessionLength < 10 then 1 else null end)/count(*)
from   playData
where  timestamp > start and timestamp < end

...但要注意count(*)= 0

相关问题