简体   繁体   English

表行中带有每行SQL SERVER 2008中的标识参数为单行

[英]Table rows with identifying parameter in each row SQL SERVER 2008 into single row

Sorry - my question title is probably as inept at my attempt to do this. 对不起 - 我的问题标题可能与我尝试这样做无关。

I have the following (well, similar) in a table in a CMS 我在CMS的表中有以下(嗯,类似)

pageID    key            value    

201       title          Page 201's title
201       description    This is 201
201       author         Dave
301       title          Page 301's title
301       description    This is 301
301       author         Bob         

As you've probably guessed, what I need is a query that will produce: 正如您可能已经猜到的那样,我需要的是一个查询,它将产生:

pageID   title              description        author

201      Page 201's title   This is page 201   Dave
301      Page 301's title   This is page 301   Bob

If anybody could help, i'd be eternally grateful - I know this is "please send me the code" but I'm absolutely stuck. 如果有人可以提供帮助,我会永远感激 - 我知道这是“请把代码寄给我”,但我绝对卡住了。

Thanks in advance. 提前致谢。

Select PageId
    , Min( Case When key = 'title' Then Value End ) As Title
    , Min( Case When key = 'description' Then Value End ) As Description
    , Min( Case When key = 'author' Then Value End ) As Author
From Table
Group By PageId

Quick hack may be 快速破解可能是

select a.pageID, a.value as Title, b.value as Description, c.value as Author from Table a
    left outer join Table b on a.pageID = b.pageID and b.key = 'description'
    left outer join Table c on a.pageID = c.pageID and c.key = 'author'
where a.key = 'title'

May not be the best solution, but it may work for you. 可能不是最好的解决方案,但它可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM