我想获得几个范围之间的计数(*)之和

时间:2013-08-09 14:21:35

标签: sql count sum between range

我想在几个范围之间得到count(*)的总和。

例如,我从一个位置范围到另一个位置范围内有10个select count(*)语句。

我想获得所有10个位置范围的总和。

SELECT count (*) 
FROM locn_hdr 
WHERE locn_brcd 
BETWEEN 'B2121' AND 'B2144'

SELECT count (*) 
FROM locn_hdr 
WHERE locn_brcd 
BETWEEN 'B2201' AND 'B2224' 

2 个答案:

答案 0 :(得分:0)

在mysql中尝试此查询:

选择(总和(if(locn_brcd介于' B2121'' B2144',1,0))+ sum(if(locn_brcd介于' B2201'和' B2224',1,0)))来自locn_hdr的总数

答案 1 :(得分:0)

如果您需要单独的结果,可以使用以下查询:

select sum(if(locn_brcd between 'B2121' and 'B2144',1,0)) as ca1, 
    sum(if(locn_brcd between 'B2201' and 'B2224',1,0)) as ca2,
    sum(if(locn_brcd between 'B2322' and 'B2344',1,0)) as ca3,
    sum(if(locn_brcd between 'B2401' and 'B2424',1,0)) as ca4,
    sum(if(locn_brcd between 'B2521' and 'B2544',1,0)) as ca5,
    sum(if(locn_brcd between 'B2601' and 'B2624',1,0)) as ca6,
    sum(if(locn_brcd between 'B0317' and 'B0340',1,0)) as ca7,
    sum(if(locn_brcd between 'B0401' and 'B0424',1,0)) as ca8,
    sum(if(locn_brcd between 'B0517' and 'B0540',1,0)) as ca9,
    sum(if(locn_brcd between 'B0601' and 'B0624',1,0)) as ca10
from locn_hd;