Rails SQL - 创建桶,并获取每个桶中的记录数

Rails SQL - create buckets, and get counts of records in each bucket

我有一个 Jobs table,其中有一列是 salary

我怎样才能将工资以 10,000 美元为单位进行分组,然后统计每个分组中有多少工作?

最好使用 Rails 活动记录的答案,但考虑到难度,我也会接受原始 SQL 答案。

起始数据

Jobs

id      salary (integer)
-----------------
1       93,530
2       72,400
3       120,403
4       193,001
...

结果数据

bucket               job_count
----------------------------
[=13=] - ,999          0
,000 - ,999    0
,000 - ,999    3
,000 - ,999    5
,000 - ,999    12

从 SQL 的角度来看,有几种方法。这是一个。

-- First, create a report table (temporarily here, but could be permanent):

create table salary_report (
  bucket      integer,
  lower_limit integer,
  upper_limit integer,
  job_count   integer
);

-- populate the table
-- note this could (and probably should) be automated, not hardcoded
insert into salary_report values (1,00000,09999,0);
insert into salary_report values (2,10000,19999,0);
insert into salary_report values (3,20000,29999,0);
insert into salary_report values (4,30000,39999,0);
insert into salary_report values (5,40000,49999,0);

-- set correct counts
update salary_report as sr
  set job_count = (
    select count(*)
    from jobs as j
    where j.salary between sr.lower_limit and sr.upper_limit
    );

-- finally, access the data (through activerecord?)
-- note: not formatted as dollar amounts
select concat( sr.lower_limit,' - ',sr.upper_limit) as range, job_count, bucket
from salary_report
order by bucket;

-- drop table if required
drop table salary_report;

我试图保持 SQL 通用,但 确切的语法可能因您的 RDBMS 而异。 没有SQLFiddle提供因为今天好像坏了

这是另一个基于 SQL 的解决方案。

像这样获取每个工资的桶:

select 
  FLOOR(salary/10000) as bucket
from jobs

使用GROUP BY进行计数:

select 
  bucket, 
  count(*)
from (
    select FLOOR(salary/10000) as bucket
    from jobs
) as t1
GROUP BY bucket

最后,添加范围而不是桶号:

select 
  CONCAT('$', FORMAT(bucket*10000,0), ' - $', FORMAT((bucket+1)*10000-1,0)) as range, 
  job_count
from ( 
    select bucket, count(*) as job_count
    from (
        select FLOOR(salary/10000) as bucket
        from jobs
    ) as t1
    GROUP BY bucket
) as t2

请注意,所使用的函数适用于 MySQL。 YMMV。