我寫一個SQL查詢來創建計算列如下:(代碼的簡化版本)讓所有在一個單行記錄在SQL Server
SELECT CUST_ID, NAME, ADDRESS,
CASE WHEN STIP_ID = 20 AND STATUS = 'OPEN' THEN 'YES' ELSE 'X' END AS COL1,
CASE WHEN STIP_ID = 21 AND STATUS = 'OPEN' THEN 'YES' ELSE 'X' END AS COL2,
CASE WHEN STIP_ID = 22 AND STATUS = 'OPEN' THEN 'YES' ELSE 'X' END AS COL3,
CASE WHEN STIP_ID = 23 AND STATUS = 'OPEN' THEN 'YES' ELSE 'X' END AS COL4,
CASE WHEN STIP_ID = 24 AND STATUS = 'OPEN' THEN 'YES' ELSE 'X' END AS COL5
FROM TABLE1
當我運行此查詢這是怎樣的報告看起來像。它會創建多個行,其中第一個條件爲多個列進行統計。我怎樣才能獲得唯一的記錄
CUSTID NAME ADDRESS Col1 Col2 Col3 Col4 Col5
101 Nikh US X X X X X
102 Chris Canada Yes X X X X
102 Chris Canada X X Yes X X
103 Todd Japan X X X Yes X
103 Todd Japan X X X X Yes
104 Levis China X X X X X
105 Jack Germany X Yes X X X
105 Jack Germany X X Yes X X
期望的結果:
CUSTID NAME ADDRESS Col1 Col2 Col3 Col4 Col5
101 Nikh US X X X X X
102 Chris Canada Yes X Yes X X
103 Todd Japan X X X Yes Yes
104 Levis China X X X X X
105 Jack Germany X Yes Yes X X
你能提供所需的數據集? – demas
你說的是將信息彙集到每個CUSTID上? –
@Kris你是對的。 –