SQL交叉表查询数字属性

时间:2016-06-13 06:15:28

标签: sql oracle

我有一个针对交叉表的SQL查询,以获取连续属性的每个bin范围的每个不同分类类别的计数。

例如:性别(男性,女性的分类属性)&年龄(连续属性)。 对于Age,binning创建为0-10,10-20,20-30,30-40 ......

所以我正在寻找查询来返回分类属性(性别)上每个范围的计数。

预期输出:特定范围:0-10

CNT          LABEL
365503       MALE
177939       FEMALE

以下查询正在运行,但一次只有一个范围,所以我必须为每个范围设置一个循环,

WITH q0 AS
  (SELECT GENDER label
  FROM IMPORTDATAAJ
  WHERE GENDER IS NOT NULL
  GROUP BY GENDER
  ORDER BY 1 ASC
  ) ,
  q1 AS
  (SELECT GENDER label
  FROM IMPORTDATAAJ
  WHERE AGE >= 0
  AND AGE   <= 1.0
  ),
  q3 AS
  (SELECT q0.label label,
    COUNT(q1.label) cnt
  FROM q0
  LEFT OUTER JOIN q1
  ON (q0.label=q1.label)
  GROUP BY q0.label
  ORDER BY label
  )
SELECT q3.label,q3.cnt FROM q3

但是我想删除这个循环来提高性能,并希望在所有范围内同时获得所有内容。

1 个答案:

答案 0 :(得分:1)

使用这样的辅助表:

select (level-1)*10 low_limit, 
        level*10 high_limit ,
        ((level-1)*10) || ' - ' || (level*10) as band
from dual connect by level <= 12

然后加入它并分组:

select band, gender, count(*)
from(
    select gender as label
    from IMPORTDATAAJ
    join (select (level-1)*10 low_limit, 
            level*10 high_limit ,
            ((level-1)*10) || ' - ' || (level*10) as band
          from dual connect by level <= 12
          )
    on age >=low_limit and age < high_limit
    )
group by band, gender
order by band, gender;