Here's a query that will work for you:
Code Snippet
-- Set up sample data
declare
@data table (AppID int, CID int, Level nvarchar(10))
insert
into @data
select
1, 100, 'a'
union
all select 1, 101, null
union
all select 1, 102, null
union
all select 2, 100, null
union
all select 2, 101, 'b'
union
all select 2, 102, null
union
all select 3, 100, null
union
all select 3, 101, null
union
all select 3, 102, 'c'
-- Query starts here (SQL Server 2005 and later - uses PIVOT)
select
AppID
, [100] as 'L1 (Level with CID 100)'
, [101] as 'L2 (Level with CID 101)'
, [102] as 'L3 (Level with CID 102)'
from
@data
pivot
(min(Level) for CID in ([100], [101], [102])) pvt
-- Alternative query starts here (all versions)
select
AppID
, min(case when CID = 100 then Level else null end) as 'L1 (Level with CID 100)'
, min(case when CID = 101 then Level else null end) as 'L2 (Level with CID 101)'
, min(case when CID = 102 then Level else null end) as 'L3 (Level with CID 102)'
from
@data
group
by AppID
There's two versions presented, depending on which version of SQL Server you're using.
Iain
-
Proposed as answer by
Cairney_MMVP
2 hours 33 minutes ago