如果INTERVAL
列总是以有效数字开头,后跟<
,您可以将其第一个值转换为数字以进行排序:
SELECT INTERVAL, COUNT(*) TOTAL
FROM (
SELECT
CASE
WHEN TA>0 AND TA<=60.00 THEN '0<TA<=60.00'
WHEN TA>60.00 AND TA<=120.00 THEN '60.00<TA<=120.00'
WHEN TA>120.00 AND TA<=180.00 THEN '120.00<TA<=180.00'
WHEN TA>180.00 AND TA<=240.00 THEN '180.00<TA<=240.00'
WHEN TA>240.00 THEN '240.00<TA'
END INTERVAL,
TA
FROM MP
)
WHERE INTERVAL IS NOT NULL
GROUP BY INTERVAL
ORDER BY TO_NUMBER(SUBSTR(INTERVAL, 1, INSTR(INTERVAL, '<') - 1));
另外,我已将 更改为HAVING Interval IS NOT NULL
,WHERE Interval IS NOT NULL
因为HAVING
它用于聚合值,例如COUNT(*)
,而不是分组值,例如INTERVAL
.
附录如果间隔的数量会有所不同,下面的查询可能更适合您。它像上面的查询一样计算间隔文本,但它可以处理任意数量的间隔。第一个CASE
条件处理范围之外的值 ( n<TA
);第二个条件处理范围 ( m<TA<=n
) 内的值。
我已经指出了您需要为每个查询设置的值。
SELECT Interval, COUNT(*) FROM (
SELECT
TA,
CASE
WHEN TA > Parm_IntSize * Parm_IntCount THEN
TO_CHAR(Parm_IntSize * Parm_IntCount) || '<TA'
ELSE
TO_CHAR(TRUNC(TA / Parm_IntSize) * Parm_IntSize)
|| '<='
|| TO_CHAR((TRUNC(TA / Parm_IntSize) + 1) * Parm_IntSize)
END AS Interval
FROM MP
CROSS JOIN (
SELECT
60 AS Parm_IntSize, -- Specify interval size here
4 AS Parm_IntCount -- Specify number of intervals here
FROM DUAL
) Parms
)
WHERE Interval IS NOT NULL
GROUP BY Interval
ORDER BY TO_NUMBER(SUBSTR(Interval, 1, INSTR(Interval, '<') - 1))