ISNULL 和 UPPER 与 GROUP BY

ISNULL and UPPER with GROUP BY

我有一个查询(精简版 - 为了清楚起见,编辑添加了 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 开发人员告诉我 ISNULL 不能与 GROUP BY 一起使用。

如何进行这项工作?

将您的查询包装在派生的 table 中,然后执行 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"

您可以通过添加整个表达式在单个查询中完成:

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");

不是 ISNULL,而是 NVL,两次 - 在 selectgroup 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");