Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I've done some googling, but I am missing something and hoping the kind folk here can help. Simply, how can I pivot the results of this query, so that each Label is a column, and the corresponding Value a value in that Label's column?
select 
	  dets.Label
	, dets.Value
from
	(select
		  sc.SubmissionPkId
		, sn.Name
		, rs.Label
		, rs.Value
	from 
		CanvasResponse rs
		inner join CanvasScreen sn on rs.ScreenPkId = sn.PkId
		inner join CanvasSection sc on sn.SectionPkId = sc.PkId
	where 
		sn.Name = 'Seller Details' 
			or sn.Name = 'Property Details'			
		and Label in 
		(
			'OwnerName', 
			'OwnetEmail', 
			'OwnerContactNumber', 
			'Price', 
			'StreetNumber', 
			'StreetName', 
			'Suburb', 
			'Town'
		)
	) dets

Funny that at database level, it's more useful as is, but it is purely for presentation, and I'd much rather pivot on the server than in WPF code.

The data, pre-pivot, looks like this, give or take a few fields. I e.g. want OwnerName , OwnetEmail (sic) etc. as columns.
OwnerName:  Sipes v Vuuren
OwnetEmail:  	
OwnerContactNumber:  079654324
OwnerID:  	
Town:  Pretoria East (South)
Suburb:  Moreleta Park
StreetName:  Paul street
StreetNumber:  123
Posted
Updated 23-Feb-15 4:02am
v2
Comments
CHill60 23-Feb-15 9:52am    
Have you got some sample data (just a few rows) and expected results?
Brady Kelly 23-Feb-15 10:02am    
Added one example record.
CHill60 23-Feb-15 10:50am    
Thanks for correcting the solution - cut&paste monster got me!

1 solution

Something like this (caveat - I haven't been able to test this properly so there might be the odd typo - used a temp table to mimic your query dets)

SQL
WITH dets as
(
select
          sc.SubmissionPkId
        , sn.Name
        , rs.Label
        , rs.Value
    from
        CanvasResponse rs
        inner join CanvasScreen sn on rs.ScreenPkId = sn.PkId
        inner join CanvasSection sc on sn.SectionPkId = sc.PkId
    where
        sn.Name = 'Seller Details'
            or sn.Name = 'Property Details'
        and Label in
        (
            'OwnerName',
            'OwnetEmail',
            'OwnerContactNumber',
            'Price',
            'StreetNumber',
            'StreetName',
            'Suburb',
            'Town'
        )

)
SELECT *
FROM (
    SELECT
        [SubmissionPkId], [Label], [Value]
    FROM dets

) as s
PIVOT
(
    MAX([Value])
    FOR [Label] IN (OwnerName, OwnetEmail, OwnerContactNumber, OwnerID, Town, Suburb, StreetName, StreetNumber )
)AS p

Give it a go and let me know where I got it wrong!
 
Share this answer
 
v3
Comments
Brady Kelly 23-Feb-15 10:50am    
It works very well, thank you, after the two small changes I made, but my big problem now is it only returns one row.
CHill60 23-Feb-15 10:52am    
Try putting brackets around (sn.Name = 'Seller Details' or sn.Name = 'Property Details') - mixing 'and' and 'or' needs brackets to define the logic
Brady Kelly 23-Feb-15 11:08am    
No diffs.
CHill60 23-Feb-15 11:30am    
Hmm. I created a spoof table to represent some stuff in dets and got 2 rows back. Can you try actually creating the dets table temporarily, check it has several rows in it and then run the pivot against the temporary dets?
Maciej Los 23-Feb-15 10:53am    
Sounds promising ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900