SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to do pivot


how to do pivot

Author
Message
venkachi
venkachi
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 41
Hi Guys,

actually my table data looks

Key value
firstname surya
firstname rakhi
firstname venkat
firstname shankar
firstname dany

I want to disply like
firstname
surya
rakhi
venkat
shankar
dany


i write the below query

SELECT firstname
FROM test pvt
PIVOT (MAX(pvt.value ) FOR pvt.key1 IN (firstname)) AS P

but it showing

firstname
venkat


Please help to solve this
s_osborne2
s_osborne2
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1887 Visits: 2292
Hi,

I haven't used PIVOT because this can be solved with a CASE statement.

DECLARE @table TABLE
(
[Key] VARCHAR(50),
Value VARCHAR(50)
)

INSERT INTO @table
SELECT 'firstname', 'surya'
UNION ALL SELECT 'firstname', 'rakhi'
UNION ALL SELECT 'firstname', 'venkat'
UNION ALL SELECT 'firstname', 'shankar'
UNION ALL SELECT 'firstname', 'dany'

SELECT
CASE WHEN [key] = 'firstname' THEN Value END AS FirstName
FROM
@table
ORDER BY
CASE WHEN [key] = 'firstname' THEN Value END



Hope that helps.

Thanks,

Simon



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
davidandrews13
davidandrews13
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1969 Visits: 4641
even easier Wink

DECLARE @table TABLE
(
[Key] VARCHAR(50),
Value VARCHAR(50)
)

INSERT INTO @table
SELECT 'firstname', 'surya'
UNION ALL SELECT 'firstname', 'rakhi'
UNION ALL SELECT 'firstname', 'venkat'
UNION ALL SELECT 'firstname', 'shankar'
UNION ALL SELECT 'firstname', 'dany'

SELECT
[value] AS FirstName
FROM
@table
ORDER BY
[Value]


s_osborne2
s_osborne2
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1887 Visits: 2292
I suspect that the OP has more than one "key" name! ;-)



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
ChrisM@home
ChrisM@home
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5536 Visits: 10611
Easier and Correcter ;-)

SELECT [firstname] = value
FROM test
WHERE key = 'firstname'




Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search