April 17, 2013 at 2:42 am
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
April 17, 2013 at 3:38 am
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 = 'firstname' THEN Value END AS FirstName
FROM
@table
ORDER BY
CASE WHEN = 'firstname' THEN Value END
Hope that helps.
Thanks,
Simon
April 17, 2013 at 3:45 am
even easier 😉
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]
April 17, 2013 at 3:47 am
I suspect that the OP has more than one "key" name! 😉
April 17, 2013 at 3:49 am
Easier and Correcter 😉
SELECT [firstname] = value
FROM test
WHERE key = 'firstname'
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply