2016-11-29 61 views
1

我有這樣的SQL查詢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

謝謝

+0

環繞你查詢了在派生表,包括案件。按彙總彙總其結果。 – jarlh

+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