簡體   English   中英

表行中帶有每行SQL SERVER 2008中的標識參數為單行

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

對不起 - 我的問題標題可能與我嘗試這樣做無關。

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

如果有人可以提供幫助,我會永遠感激 - 我知道這是“請把代碼寄給我”,但我絕對卡住了。

提前致謝。

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

快速破解可能是

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'

可能不是最好的解決方案,但它可能對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM