sql - 在范围内分组以包括没有值的范围

时间:2012-05-15 17:20:46

标签: sql postgresql

假设一个与此question类似的场景。我想得到以下结果:

score range  | number of occurrences
-------------------------------------
   0-9       |        11
  10-19      |        14
  20-29      |         3
   ...       |       ...

我可以使用所选答案作为解决方案:

select t.range as [score range], count(*) as [number of occurences]
from (
  select case  
    when score between 0 and 9 then ' 0- 9'
    when score between 10 and 19 then '10-19'
    else '20-99' end as range
  from scores) t
group by t.range

我怎样才能确保即使在该范围内没有结果,也会显示30-39的分数范围?

4 个答案:

答案 0 :(得分:22)

尝试此查询(也在SQL Fiddle上):

WITH ranges AS (
    SELECT (ten*10)::text||'-'||(ten*10+9)::text AS range,
           ten*10 AS r_min, ten*10+9 AS r_max
      FROM generate_series(0,9) AS t(ten))
SELECT r.range, count(s.*)
  FROM ranges r
  LEFT JOIN scores s ON s.score BETWEEN r.r_min AND r.r_max
 GROUP BY r.range
 ORDER BY r.range;

修改

您可以通过将参数更改为generate_series()来轻松调整范围。可以使用以下构造来确保ranges始终涵盖您的分数:

SELECT (ten*10)::text||'-'||(ten*10+9)::text AS range,
       ten*10 AS r_min, ten*10+9 AS r_max
  FROM generate_series(0,(SELECT max(score)/10 FROM scores)) AS t(ten))

用于ranges CTE。

答案 1 :(得分:1)

你不能喜欢这样,但是如果你用范围添加派生表就可以了:

select ranges.range, count(scores.score) as [number of occurences]
  from
  (
     select 0 minRange, 9 maxRange, '0-9' range
     union all
     select 10, 19, '10-19'
     union all
     select 20, 29, '20-29'
  ) ranges
  left join scores
    on scores.score between ranges.minRange and ranges.maxRange  
 group by ranges.range

不确定postgresql的语法。

编辑:语法正确:

select ranges."range", count(scores.score) as "number of occurences"
  from
  (
     select 0 minRange, 9 maxRange, '0-9' "range"
     union all
     select 10, 19, '10-19'
     union all
     select 20, 29, '20-29'
  ) as ranges
  left join scores
    on scores.score between ranges.minRange and ranges.maxRange  
 group by ranges.range

答案 2 :(得分:1)

select r.range as [score range], count(*) as [number of occurences]
from 
    (
    select ' 0- 9' as range, 9 as endrange
    union all select '10-19',19
    union all select '20-29',29
    union all select '30-39',39
    union all select '40-49',49
    union all select '50-59',59
    union all select '60-69',69
    union all select '70-79',79
    union all select '80-89',89
    union all select '90-99',99
    ) as r
left join scores s on 
    r.endrange = case 
    when s.score > 90 then 99
    when s.score > 80 then 89
    when s.score > 70 then 79
    when s.score > 60 then 69
    when s.score > 50 then 59
    when s.score > 40 then 49
    when s.score > 30 then 39
    when s.score > 20 then 29
    when s.score > 10 then 19
    when s.score > 0 then 9
    end
group by r.range

答案 3 :(得分:0)

实际上,最简单的解决方案是(2位数字):

select substr(rssi::text, 0, 2) || '0-' || substr(rssi::text, 0, 2) || '9' as range, count(*)
from sensor_stations
group by substr(rssi::text, 0, 2)
order by count(*) desc;

输出将是这样的:

 range | count 
-------+-------
 10-19 |  3414
 30-39 |  1363
 20-29 |  1269
 40-49 |   769
 50-59 |   294
 60-69 |   106
 70-79 |     5
(7 rows)