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

时间:2010-04-28 23:34:30

标签: sql sql-server tsql

抱歉 - 我的问题标题可能与我尝试这样做无关。

我在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         

正如您可能已经猜到的那样,我需要的是一个将产生的查询:

pageID   title              description        author

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

如果有人可以提供帮助,我会永远感激 - 我知道这是“请把代码寄给我”,但我绝对卡住了。

提前致谢。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

快速入侵可能

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'

可能不是最佳解决方案,但它可能适合您。