我有一个查询(精简版-为了清楚起见,已编辑以添加SUM项)-
SELECT STORENBR AS "Store",
DISPENSER AS "Model",
UPPER(TINTERSERIALNBR) AS "Serial",
SUM (CASE WHEN CLRNTSYS ='CCE' THEN GALLONS ELSE 0 END) AS "2018 CCE gallons",
SUM (CASE WHEN CLRNTSYS ='BAC' THEN GALLONS ELSE 0 END) AS "2018 BAC gallons",
SUM (CASE WHEN CLRNTSYS ='844' THEN GALLONS ELSE 0 END) AS "2018 844 gallons"
FROM table
GROUP BY STORENBR, DISPENSER, UPPER(TINTERSERIALNBR);
工作正常。但是现在我必须在“序列号”字段中考虑NULL值。我尝试过
SELECT STORENBR AS "Store",
DISPENSER AS "Model",
ISNULL(UPPER(TINTERSERIALNBR), "N/A") AS "Serial",
SUM (CASE WHEN CLRNTSYS ='CCE' THEN GALLONS ELSE 0 END) AS "2018 CCE gallons",
SUM (CASE WHEN CLRNTSYS ='BAC' THEN GALLONS ELSE 0 END) AS "2018 BAC gallons",
SUM (CASE WHEN CLRNTSYS ='844' THEN GALLONS ELSE 0 END) AS "2018 844 gallons"
FROM table
GROUP BY STORENBR, DISPENSER, UPPER(TINTERSERIALNBR);
并且Oracle SQL Developer告诉我ISNULL不能与GROUP BY一起使用。
我如何进行这项工作?
答案 0 :(得分:5)
您可以在单个查询中添加整个表达式:
SELECT STORENBR AS "Store",
DISPENSER AS "Model",
NVL(UPPER(TINTERSERIALNBR), "N/A") AS "Serial",
SUM (CASE WHEN CLRNTSYS ='CCE' THEN GALLONS ELSE 0 END) AS "2018 CCE gallons",
SUM (CASE WHEN CLRNTSYS ='BAC' THEN GALLONS ELSE 0 END) AS "2018 BAC gallons",
SUM (CASE WHEN CLRNTSYS ='844' THEN GALLONS ELSE 0 END) AS "2018 844 gallons"
FROM table
GROUP BY STORENBR, DISPENSER, NVL(UPPER(TINTERSERIALNBR), "N/A");
答案 1 :(得分:2)
不是ISNULL
,而是NVL
,两次-在select
和group by
语句中
SELECT STORENBR AS "Store",
DISPENSER AS "Model",
NVL(UPPER(TINTERSERIALNBR), "N/A") AS "Serial",
SUM (CASE WHEN CLRNTSYS ='CCE' THEN GALLONS ELSE 0 END) AS "2018 CCE gallons",
SUM (CASE WHEN CLRNTSYS ='BAC' THEN GALLONS ELSE 0 END) AS "2018 BAC gallons",
SUM (CASE WHEN CLRNTSYS ='844' THEN GALLONS ELSE 0 END) AS "2018 844 gallons"
FROM table
GROUP BY STORENBR, DISPENSER, NVL(UPPER(TINTERSERIALNBR), "N/A");
答案 2 :(得分:0)
在派生表中结束查询,然后执行GROUP BY:
select "Store",
"Model",
"Serial",
SUM (CASE WHEN CLRNTSYS ='CCE' THEN GALLONS ELSE 0 END) AS "2018 CCE gallons",
SUM (CASE WHEN CLRNTSYS ='BAC' THEN GALLONS ELSE 0 END) AS "2018 BAC gallons",
SUM (CASE WHEN CLRNTSYS ='844' THEN GALLONS ELSE 0 END) AS "2018 844 gallons"
from
(
SELECT STORENBR AS "Store",
DISPENSER AS "Model",
ISNULL(UPPER(TINTERSERIALNBR), "N/A") AS "Serial",
CLRNTSYS,
GALLONS
FROM table
) dt
GROUP BY "Store", "Model", "Serial"