开发者

How to return varchar string instead of null with rollup?

开发者 https://www.devze.com 2023-02-19 10:00 出处:网络
When I run this query: select First_Name, sum(a) a, sum(b) b, 开发者_如何学Csum(c) c from someTable group by First_Name with rollup

When I run this query:

select
    First_Name,
    sum(a) a,
    sum(b) b,
  开发者_如何学C  sum(c) c
from someTable
group by First_Name with rollup

the first column First_Name and the last row (total row) shows a NULL (since it can't be summed), I want to show a custom string value instead of null. I know I can use isnull or a case here, but I want only the rolled up row to be affected.

Thanks!


SELECT  CASE WHEN GROUPING_ID(First_Name) = 1 THEN 'Total' ELSE First_Name END,
        SUM(a),
        SUM(b),
        SUM(c)
FROM    data
GROUP BY
        First_Name WITH ROLLUP


CASE WHEN GROUPING(First_Name) = 1 THEN 'FOO' ELSE First_Name END AS First_Name

0

精彩评论

暂无评论...
验证码 换一张
取 消