2017-03-06 6 views
1

結果行3と行4を1行レコードに結合できますか?等(ストアプロシージャ)結果を2行と1行の結果にまとめる

select @fYear as [Year], 
    (case when main.Description in ('Kecil', 'Tanah') then 'JK' 
     else main.Description 
    end) as description, 

    --CardType, 
    sum(case when MONTH(blue.AppliedDate) = 1 then 1 else 0 end) as Jan_Collection, 
    sum(case when MONTH(blue.AppliedDate) = 2 then 1 else 0 end) as Feb_Collection, 
    ... 

    from tblR as main 
    left join tblP as b on main.requestorid = b.requestorid 
    left join tblB as blue on b.partyid = blue.partyid and YEAR(blue.AppliedDate) = @fYear 

    group by (case when main.Description in ('Kecil', 'Tanah') then 'JK' 
      else main.Description 
     end) 

この出力: https://gyazo.com/d930cb2aee92f90ba31dd543d6ca64f3

しかし、私は、この絵のようなJKのような1つのレコードに、レコードの行3と4を組み合わせて表示することができる:https://gyazo.com/a89ed2fa04b51135bf8601d59d4af0b2

おかげ。あなたがそれらの特定のdescriptionsを結合したい場合は

+0

は、そのグループの背後にはロジックがありません。それとも少なくとも私はそれを見ることができません –

答えて

1

、あなたはselectgroup byの両方で、caseを使用することができます。

select @fYear as [Year], 
     (case when description in ('kecil', 'Tanah') then 'JK' 
      else main.Description 
     end) as Description, 
     sum(case when MONTH(blue.AppliedDate) = 1 then 1 else 0 end) as Jan_Collection, 
     sum(case when MONTH(blue.AppliedDate) = 2 then 1 else 0 end) as Feb_Collection, 
     ... 
from tblR main left join 
    tblP b 
    on main.requestorid = b.requestorid left join 
    tblB 
    blue 
    on b.partyid = blue.partyid and YEAR(blue.AppliedDate) = @fYear 
group by (case when description in ('kecil', 'Tanah') then 'JK' 
       else main.Description 
      end) 
+0

が言及..私のために働く。ありがとう –

+0

Berhad、Tinggi、JKによる注文方法?私は 'を追加します(ケースmain.Description'Berhad 'の場合は1 ' Tinggi 'のとき2 ' JK 'のとき3 ELSEの説明END)ASC' –

関連する問題