开发者

Need help with pivot for this table sql server

开发者 https://www.devze.com 2023-03-20 19:04 出处:网络
I have a table with the following structure uidsideidKeyvalue 111F.NameSimon 211L.NameJones 311C.NameAAPL

I have a table with the following structure

uid  sid  eid   Key     value
1    1     1   F.Name   Simon 
2    1     1   L.Name   Jones
3    1     1   C.Name   AAPL
4    1     1   Address  Infinite
5    2     1   F.Name   Brad
6    2     1   L.Name   Pitt
7    2     1   C.Name   Holly
8    2     1   Address  LA

I would like to convert the above table to below format

sid  F.Name  L.Name  C.Name  Address
1    Simon   Jones   AAPL    Infinite
2    Brad    Pitt    Holly   LA

Basically I need values of开发者_运维问答 "Key" column to be column fields in new table. I have no other table. Even Linq to sql is ok and I can understand it.


In 2005 and later:

SELECT  *
FROM    (
        SELECT  sid, key, value
        FROM    mytable
        ) q
PIVOT   (
        MIN(value)
        FOR
        key IN ([F.Name], [L.Name], [C.Name], [Address])
        ) p
0

精彩评论

暂无评论...
验证码 换一张
取 消