2015-09-29 83 views
0

想法是这样的,我有一列的值范围从0到25,000。将SQL查询中的一列拆分为多列

我想主要有一列计数的值从0到5000,5000-10000数等

会在哪里开始呢?我将如何做这样的事情?

回答

2

我假设你真的不想要范围重叠:

count(case when <column> between  0 and 5000 then 1 else null end) as range0, 
count(case when <column> between 5001 and 10000 then 1 else null end) as range1, 
... 

也许你喜欢(如果你真的意味着0〜4999,5000〜9999等):

count(case when <column>/5000 = 0 then 1 else null end) as range0, 
count(case when <column>/5000 = 1 then 1 else null end) as range1, 
count(case when <column>/5000 = 2 then 1 else null end) as range2, 
count(case when <column>/5000 = 3 then 1 else null end) as range3, 
count(case when <column>/5000 = 4 then 1 else null end) as range4, 
count(case when <column>/5000 = 5 then 1 else null end) as range5 

你需要最后一个实际覆盖25,000或只是做>= 4。并注意我假定了整数除法。

+0

啊,案例陈述。我完全忘了这些,谢谢。 – Leon

+0

@Leon:根据您的DBMS,您也可以使用过滤的聚合:例如'count(*)filter(/5000 = 0)' –