स्टेटिक पिवट (देखें SQL Fiddle for Demo ):
select id, [user], [engineer], [manu], [OS]
from
(
select t.id
, t.[user]
, p.ticketid
, p.label
, p.value
from tickets t
inner join properties p
on t.id = p.ticketid
) x
pivot
(
min(value)
for label in ([engineer], [manu], [OS])
) p
या आप एक डायनामिक पिवट का उपयोग कर सकते हैं (देखें SQL Fiddle for Demo ):
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(p.label)
from tickets t
inner join properties p
on t.id = p.ticketid
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT id, [user], ' + @cols + ' from
(
select t.id
, t.[user]
, p.ticketid
, p.label
, p.value
from tickets t
inner join properties p
on t.id = p.ticketid
) x
pivot
(
min(value)
for label in (' + @cols + ')
) p '
execute(@query)
दोनों क्वेरी समान परिणाम लौटाएंगी।