tags:

views:

37

answers:

1

Hi I'm a newby to SQL code and wondered if it was possible to get an output of:-

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

From the below data source.

**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  A: 
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
Martin Smith
brilliant thanks
Stephen Graham
Is there any way to exclude records where one of the items is a NULL?
Stephen Graham