2010-10-11 79 views
2

嗨,我是一個牛逼到SQL代碼,並懷疑這是可能得到的輸出: -SQL查詢 - 在不同的項目比較兩個項目

 
**EVT John Paul Difference** 
A1  1  2  -1 
A2  2  3  -1 

從下面的數據源。

 
**EVT PERS RANK** 
A1  John 1 
A1  Paul 2 
A1  Ringo 3 
A1  George 4 
A2  Ringo 1 
A2  John 2 
A2  Paul 3 
A2  George 4 

回答

1
SELECT 
     EVT, 
     MAX(CASE WHEN Pers='John' THEN Rank END) AS John, 
     MAX(CASE WHEN Pers='Paul' THEN Rank END) AS Paul, 
     MAX(CASE WHEN Pers='John' THEN Rank END) - 
        MAX(CASE WHEN Pers='Paul' THEN Rank END) as Difference 
FROM YourTable 
WHERE Pers IN ('John','Paul') 
GROUP BY EVT 
+0

輝煌感謝 – Spufferoo 2010-10-11 10:47:35

+0

有什麼辦法來排除記錄,其中的項目之一是NULL? – Spufferoo 2010-10-19 21:06:27