如何使用 CASE/WHEN 对 SQL 中的范围值进行分类?如果不可能,还有其他选择吗?
基本上我希望能够将所选的列分类到不同的范围,如下所示: 如果范围>大于 0 返回列的值。 如果范围<大于 0 返回“已过期”。 这是代码:
SELECT
PACKAGE_ID,
PACKAGE_NAME,
ORG_ID,
ORG_NAME,
VALID_FROM,
VALID_TO,
DAYS_LEFT,
TOTAL,
BALANCE,
THRESHOLD,
REPORTID
FROM
( SELECT
B.ORG_ID,
B.ORG_NAME,
A.PACKAGE_ID,
A.NAME AS PACKAGE_NAME,
A.BALANCE AS BALANCE,
A.VALID_FROM,
A.VALID_TO,
C.REPORT_CODE,
Floor(A.VALID_TO - (SELECT current_date FROM dual)) AS DAYS_LEFT,
X.TOTAL AS TOTAL,
CASE X.TOTAL WHEN 0 THEN 0 ELSE A.BALANCE / X.TOTAL* 100 END AS THRESHOLD,
'MIS0014' AS REPORTID
FROM
PREPAID_PACKAGE A
JOIN MST_ORGANISATION B ON B.ORG_ID = A.ORG_ID
JOIN PREPAID_REPORT C ON C.PACKAGE_ID = A.PACKAGE_ID
JOIN (
SELECT
SUM(Z.TOPUP_VALUE) as TOTAL,
Y.ORG_ID AS ORG_ID,
Y.PACKAGE_ID AS PACKAGE_ID
from PREPAID_VALUE Z
JOIN PREPAID_PACKAGE Y on Y.PACKAGE_ID =Z.PACKAGE_ID
GROUP BY Y.ORG_ID, Y.PACKAGE_ID
) X ON X.ORG_ID=A.ORG_ID AND X.PACKAGE_ID=A.PACKAGE_ID
ORDER BY
ORG_CODE,
PACKAGE_ID,
PACKAGE_NAME,
C.REPORT_CODE) S
WHERE
REPORTID='MIS0014'
AND ORG_ID = 1
AND VALID_FROM >= TO_DATE('01/02/2007', 'dd/mm/yyyy')
AND VALID_TO < TO_DATE('01/02/2013', 'dd/mm/yyyy')
AND THRESHOLD < 20
AND DAYS_LEFT < 13
GROUP BY
PACKAGE_ID,
PACKAGE_NAME,
ORG_ID,
ORG_NAME,
DAYS_LEFT,
VALID_FROM,
VALID_TO,
REPORTID,
TOTAL,
BALANCE,
THRESHOLD,
REPORTID
我想将以下语句:更改
Floor(A.VALID_TO - (SELECT current_date FROM dual)) AS DAYS_LEFT,
为以下:
when Floor(A.VALID_TO - (SELECT current_date FROM dual)) > 0 then Floor(A.VALID_TO - (SELECT current_date FROM dual)) else 'Expired' end AS DAYS_LEFT,
或类似的内容, 是否有任何关键字/函数可以让我这样做?
Basically I want to be able to categorize the column selected into different range like so:
if the range > than 0 return the value of the column.
if the range < than 0 return 'Expired'.
Here is the code:
SELECT
PACKAGE_ID,
PACKAGE_NAME,
ORG_ID,
ORG_NAME,
VALID_FROM,
VALID_TO,
DAYS_LEFT,
TOTAL,
BALANCE,
THRESHOLD,
REPORTID
FROM
( SELECT
B.ORG_ID,
B.ORG_NAME,
A.PACKAGE_ID,
A.NAME AS PACKAGE_NAME,
A.BALANCE AS BALANCE,
A.VALID_FROM,
A.VALID_TO,
C.REPORT_CODE,
Floor(A.VALID_TO - (SELECT current_date FROM dual)) AS DAYS_LEFT,
X.TOTAL AS TOTAL,
CASE X.TOTAL WHEN 0 THEN 0 ELSE A.BALANCE / X.TOTAL* 100 END AS THRESHOLD,
'MIS0014' AS REPORTID
FROM
PREPAID_PACKAGE A
JOIN MST_ORGANISATION B ON B.ORG_ID = A.ORG_ID
JOIN PREPAID_REPORT C ON C.PACKAGE_ID = A.PACKAGE_ID
JOIN (
SELECT
SUM(Z.TOPUP_VALUE) as TOTAL,
Y.ORG_ID AS ORG_ID,
Y.PACKAGE_ID AS PACKAGE_ID
from PREPAID_VALUE Z
JOIN PREPAID_PACKAGE Y on Y.PACKAGE_ID =Z.PACKAGE_ID
GROUP BY Y.ORG_ID, Y.PACKAGE_ID
) X ON X.ORG_ID=A.ORG_ID AND X.PACKAGE_ID=A.PACKAGE_ID
ORDER BY
ORG_CODE,
PACKAGE_ID,
PACKAGE_NAME,
C.REPORT_CODE) S
WHERE
REPORTID='MIS0014'
AND ORG_ID = 1
AND VALID_FROM >= TO_DATE('01/02/2007', 'dd/mm/yyyy')
AND VALID_TO < TO_DATE('01/02/2013', 'dd/mm/yyyy')
AND THRESHOLD < 20
AND DAYS_LEFT < 13
GROUP BY
PACKAGE_ID,
PACKAGE_NAME,
ORG_ID,
ORG_NAME,
DAYS_LEFT,
VALID_FROM,
VALID_TO,
REPORTID,
TOTAL,
BALANCE,
THRESHOLD,
REPORTID
I'd like to change the following statement :
Floor(A.VALID_TO - (SELECT current_date FROM dual)) AS DAYS_LEFT,
to the following:
when Floor(A.VALID_TO - (SELECT current_date FROM dual)) > 0 then Floor(A.VALID_TO - (SELECT current_date FROM dual)) else 'Expired' end AS DAYS_LEFT,
or something similar,
Is there any keyword/function to allow me to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
既然您已经有了内联查询,为什么不在主 SELECT 中执行它呢?
例如
Since you've already got an inline query why not just do it in your main SELECT.
e.g.
您似乎已经几乎正确了...但这是正确的语法:
如果 Oracle 抱怨您不匹配数据类型,则可能意味着您需要转换
FLOOR(A.VALID_TO - SYSDATE)
通过将其包装在CAST
函数中来转换为varchar2
:CAST(FLOOR(A.VALID_TO - SYSDATE) AS VARCHAR2(5))
- 但是这可能根本没有必要。You seem to have it almost correct already... but here's the correct syntax:
In case Oracle complains you about not matching datatypes, it may mean that you need to cast the
FLOOR(A.VALID_TO - SYSDATE)
tovarchar2
by wrapping it inside aCAST
function:CAST(FLOOR(A.VALID_TO - SYSDATE) AS VARCHAR2(5))
- but that may not be necessary at all.