我正在组装一个查询以在JFreeChart中显示一个实验。查询工作正常,但不是在JFreeChart.Its程序集中显示间隔为字符串(区间如60bigger比TAsmaller than120是图表中的最后一个,应该是第二个)。我将举例说明每个60分钟的五个间隔(TA是一个数字字段,意味着时间平均):
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) GROUP BY INTERVAL HAVING INTERVAL IS NOT NULL ORDER BY INTERVAL
我该怎么做才能正确显示间隔而不会破坏/破坏我的查询,因为它会根据用户的选择即时组装。
答案 0 :(得分:0)
如果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))