ROLLUP function; Replace NULL with β€œTotal” with the data type of the INT column not VARCHAR

I'm having trouble replacing my ROLLUP NULL with a string value, because my column data type is Integer.

SELECT CASE
WHEN GROUPING(Column1) = 1 THEN 'Total'
ELSE Column1
END  Column1, SUM(Column2) AS MySum
FROM MyTable
GROUP BY Column1 WITH ROLLUP;

I can put a numerical value in:

WHEN GROUPING(Column1) = 1 THEN '9999'

but I can’t figure out how to convert to varchar if the value is NULL and then replace it with β€œTotal”.

+3
source share
1 answer

Test Data

DECLARE @MyTable TABLE (Column1 INT,Column2 INT)
INSERT INTO @MyTable VALUES
(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)

SELECT CASE
         WHEN GROUPING(Column1) = 1 THEN 'Total'
         ELSE CAST(Column1 AS VARCHAR(10))     --<-- Cast as Varchar
       END  Column1
      , SUM(Column2) AS MySum
FROM @MyTable
GROUP BY Column1 
WITH ROLLUP;

Result set

╔═════════╦═══════╗
β•‘ Column1 β•‘ MySum β•‘
╠═════════╬═══════╣
β•‘ 1       β•‘     6 β•‘
β•‘ 2       β•‘     6 β•‘
β•‘ 3       β•‘     6 β•‘
β•‘ Total   β•‘    18 β•‘
β•šβ•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•

Note

The reason you could not do what you tried to do is because when using the operator CASEin each case, the returned data type must be the same.

In the above query, I only have CAST colum1 in varchar and it worked.

+2

All Articles