带有别名的SQL Rollup

时间:2016-11-29 13:32:16

标签: sql postgresql

我有这个SQL查询

Select 
  Case 
    When transfer.trf_type = 'c' then 'Transfer to own card' 
    When transfer.trf_type = 'o' then 'Transfer to own account' 
    When transfer.trf_type = 'I' then 'Transfer to a domestic bank' 
    When transfer.trf_type = 'b' then 'Transfer to another AIIB Customer' 
  End As Type , 
  Count(transfer.trf_type) As total, 
  Sum (transfer.amount*currency.rate) AS totalSum 
From transfer 
Inner Join currency on transfer.currency = currency.currency 
Where transfer.to_card IS null 
Group By Rollup(Type)

它给了我带和的结果集,但是空格。

我希望汇总行显示一个特定的别名,例如:“总资金转移”。我如何实现这一目标?

这是我的查询结果,我需要在最后一行添加总计

enter image description here

谢谢

1 个答案:

答案 0 :(得分:2)

试试这个

   with grpSum as (Select
          Case 
        When transfer.trf_type = 'c' then 'Transfer to own card' 
        When transfer.trf_type = 'o' then 'Transfer to own account' 
        When transfer.trf_type = 'I' then 'Transfer to a domestic bank' 
        When transfer.trf_type = 'b' then 'Transfer to another AIIB Customer' 
      End As Type , 
      Count(transfer.trf_type) As total, 
      Sum (transfer.amount*currency.rate) AS totalSum 
    From transfer 
    Inner Join currency on transfer.currency = currency.currency 
    Where transfer.to_card IS null
        Group By ROLLUP(Type))
        select COALESCE(Type,'Total found transfers'),total,totalSum from grpSum