0

I have a table with id, desc, quantity. I used GROUP BY WITH ROLLUP to get the subTotal in the result. Here is SQL Fiddle link

In the result i want to know how to make the "description" column blank for the row created by roll up

In my actual scenario i have other columns also, but rolled up only on one column. So other columns should be empty.

My Result should be like below.

COALESCE(ID,'TOTAL')    DESCRIPTION                 SUM

1                       Chocolate Chip Cookies      17
2                       Oatmeal Cookies             33
3                       Snaker Cookies              49
TOTAL                                               99
Rao
  • 2,902
  • 14
  • 52
  • 70

2 Answers2

2

Here is one method:

SELECT COALESCE(id,'TOTAL'),
       (case when id is null then null else description end) as description,
       sum(qty) AS SUM
FROM item
group by id WITH ROLLUP;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
-1

SELECT c.printable_name AS 'Country', count(*) AS '#' FROM registrations r INNER JOIN country c ON r.country = c.country_id GROUP BY country

  • 1
    While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run. – Bono May 16 '16 at 14:25