久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

根據列中的值將單行轉換為多行

Converting single row into multiple rows based on values in columns(根據列中的值將單行轉換為多行)
本文介紹了根據列中的值將單行轉換為多行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個表格,其中每條記錄代表一個人,并且有許多列用于表明他們參加了哪些活動:

I have a table where each record represents a person and there are many columns used to indicate what events they attended:

CREATE TABLE EventAttendees
(
    Person VARCHAR(100),
    [Event A] VARCHAR(1),
    [Event B] VARCHAR(1),
    [Event C] VARCHAR(1)
)

INSERT INTO EventAttendees
SELECT 'John Smith','x',NULL,NULL
UNION
SELECT 'Jane Doe',NULL,'x','x'
UNION
SELECT 'Phil White','x',NULL,'x'
UNION
SELECT 'Sarah Jenkins','x','x','x'

例如:

SELECT * FROM Event Attendees

/---------------|---------|---------|---------\
| Person        | Event A | Event B | Event C |
|---------------|---------|---------|---------|
| John Smith    |    x    |   NULL  |   NULL  |
| Jane Doe      |   NULL  |    x    |    x    |
| Phil White    |    x    |   NULL  |    x    |
| Sarah Jenkins |    x    |    x    |    x    |
\---------------|---------|---------|---------/

我想生成一個誰參加了哪些活動的列表,所以我想要的輸出是:

I want to generate a list of who attended which events, so my desired output is:

/---------------|---------|
| Person        | Event   |
|---------------|---------|
| John Smith    | Event A |
| Jane Doe      | Event B |
| Jane Doe      | Event C |
| Phil White    | Event A |
| Phil White    | Event C |
| Sarah Jenkins | Event A |
| Sarah Jenkins | Event B |
| Sarah Jenkins | Event C |
\---------------|---------/

實際上我有超過 3 個事件,但以上是為了便于解釋(順便說一下,這不是作業問題).由于事件將來可能會發生變化,而且我無法控制傳遞的數據,因此我確實需要一個可以處理任意數量的可能事件列的動態解決方案.

In reality I have many more than 3 events, but the above is for ease of explanation (This is not a homework question btw). As the Events might change in the future and I have no control over the data I am being passed, I really need a dynamic solution which can handle any number of possible event columns.

我假設我可以用 UNPIVOT 做一些事情,但我就是想不通,或者在 SO 或其他地方找到一個很好的例子 - 有人可以幫忙嗎?

I'm assuming I can do something with UNPIVOT, but I just can't figure it out, or find a good example on SO or elsewhere to work from - can someone help?

推薦答案

想出了我想到的解決方案,但是是的,它確實需要動態 SQL 來獲取相關列名以輸入 UNPIVOT:

Figured out the solution I was thinking of, but yes, it does require dynamic SQL to get the relevant column names to feed into the UNPIVOT:

declare @sql varchar(max)
set @sql = 
    'select Person, EventName
    from EventAttendees
    unpivot
    (
        Attended for EventName in (' + (select
                                        stuff((
                                            select ',' + QUOTENAME(c.[name])
                                            from sys.columns c
                                            join sys.objects o on c.object_id = o.object_id
                                            where o.[name] = 'EventAttendees'
                                            and c.column_id > 1
                                            order by c.[name]
                                            for xml path('')
                                        ),1,1,'') as colList) + ')
    ) unpiv
    where unpiv.Attended = ''x''
    order by Person, EventName'

exec (@sql)

在這個例子中,我假設事件列從表中的第二列開始,但顯然我可以在子查詢中使用一些不同的邏輯來識別相關列.

In this example, I am making the assumption that the Event columns are from the second column in the table onwards, but obviously I could use some different logic within the subquery to identify the relevant columns if necessary.

在我的示例數據上,這給出了所需的結果:

On my example data, this gives the desired result:

/---------------------------\
| Person        | EventName |
|---------------|-----------|
| Jane Doe      | Event B   |
| Jane Doe      | Event C   |
| John Smith    | Event A   |
| Phil White    | Event A   |
| Phil White    | Event C   |
| Sarah Jenkins | Event A   |
| Sarah Jenkins | Event B   |
| Sarah Jenkins | Event C   |
\---------------------------/

我想我更喜歡這個而不是使用游標,盡管我實際上還沒有確認這兩種動態方法之間有什么性能差異(如果有的話).

I think I prefer this to using a cursor, although I haven't actually confirmed what performance difference (if any) there is between the two dynamic approaches.

感謝大家對這個問題的幫助和建議,一如既往地非常感謝!

Thanks for everyone's help and suggestions on this question though, greatly appreciated as always!

這篇關于根據列中的值將單行轉換為多行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
Converting the name of a day to its integer representation(將一天的名稱轉換為其整數表示)
How to convert nvarchar m/d/yy to mm/dd/yyyy in SQL Server?(如何在 SQL Server 中將 nvarchar m/d/yy 轉換為 mm/dd/yyyy?)
主站蜘蛛池模板: 国产精品久久久久久久一区二区 | 91福利在线观看 | 日韩在线观看中文字幕 | 国产精品久久久久久久久久久免费看 | 在线视频一区二区 | 免费毛片网站 | 成av在线 | 九色一区 | 正在播放国产精品 | 综合久久av| 亚洲一区二区三区福利 | 视频在线亚洲 | 一区二区三区播放 | av天天干 | 91久久夜色精品国产网站 | 91精品国模一区二区三区 | 欧美8一10sex性hd | 亚洲人人| 成年视频在线观看 | 久久网站免费视频 | 亚洲另类春色偷拍在线观看 | 中文字幕在线免费视频 | 成人免费福利视频 | 国产a级黄色录像 | 久久精品在线 | 精品99在线| 韩国电影久久 | 成人小视频在线观看 | 日韩一区中文字幕 | 日韩亚洲一区二区 | 成人免费网站www网站高清 | 国产一区二区三区四区三区四 | 91在线免费观看网站 | 日韩精品一二三区 | 久亚州在线播放 | 你懂的免费在线 | 色一阁 | 做a视频| 欧洲妇女成人淫片aaa视频 | 日韩一区二区三区四区五区 | 欧美日韩久久久久 |