我正在尝试在select语句中使用大小写条件,但是它没有给出正确的结果,但是当我在where子句中使用该条件时,它会起作用。
以下查询有效:
SELECT 'Postpaid' AS 'Description',
SUM(ROUND(duration/60,3)) AS Minutes
FROM cdr WHERE typeofcall IN ('0800 Voice Incoming Off Peak','0800 Voice Incoming Peak')
AND
(callingparty LIKE '67%' OR callingparty LIKE '28%' OR callingparty LIKE '38%'
OR callingparty LIKE '48%' OR callingparty LIKE '58%'
OR callingparty LIKE '78%' OR callingparty LIKE '88%')
AND FROM_UNIXTIME(timeofcall,'%Y-%m-%d %h:%i:%s') >='2019-05-01'
AND FROM_UNIXTIME(timeofcall,'%Y-%m-%d %h:%i:%s') <='2019-05-31'
AND charge >0
Description Minutes
----------- ---------
Postpaid 198.351
但是我的要求是该查询没有给出结果:
SELECT 'Postpaid' AS 'Description',
(CASE WHEN callingparty LIKE '67%' OR callingparty LIKE '28%' OR callingparty LIKE '38%'
OR callingparty LIKE '48%' OR callingparty LIKE '58%'
OR callingparty LIKE '78%' OR callingparty LIKE '88%'
THEN SUM(ROUND(duration/60,3)) ELSE 0 END )AS Minutes
FROM cdr WHERE typeofcall IN ('0800 Voice Incoming Off Peak','0800 Voice Incoming Peak')
AND FROM_UNIXTIME(timeofcall,'%Y-%m-%d %h:%i:%s') >='2019-05-01'
AND FROM_UNIXTIME(timeofcall,'%Y-%m-%d %h:%i:%s') <='2019-05-31'
AND charge >0
Description Minutes
----------- ---------
Postpaid 0.000
答案 0 :(得分:2)
我会改变大小写,并将和和舍入放在外面:
SELECT 'Postpaid' AS 'Description',
round(sum((CASE WHEN callingparty LIKE '67%' OR callingparty LIKE '28%' OR callingparty LIKE '38%'
OR callingparty LIKE '48%' OR callingparty LIKE '58%'
OR callingparty LIKE '78%' OR callingparty LIKE '88%'
THEN duration/60 ELSE 0 END) ),3) AS Minutes
FROM cdr WHERE typeofcall IN ('0800 Voice Incoming Off Peak','0800 Voice Incoming Peak')
AND FROM_UNIXTIME(timeofcall,'%Y-%m-%d %h:%i:%s') >='2019-05-01'
AND FROM_UNIXTIME(timeofcall,'%Y-%m-%d %h:%i:%s') <='2019-05-31'
AND charge >0
如果您的条件满足,则需要对这些值求和,但如果不满足,则需要求和0