久久久久久久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?)
主站蜘蛛池模板: 精品国产一区二区三区久久久蜜月 | 男人天堂99 | 福利在线观看 | 国产99视频精品免费视频7 | 久久久精品影院 | 午夜影院黄| 日韩美香港a一级毛片免费 国产综合av | 国产精品久久久久久久久免费桃花 | 中文字幕在线精品 | 作爱视频免费看 | 欧美色综合天天久久综合精品 | 91大片| 国内精品视频在线观看 | 99re国产视频 | 国产日韩欧美在线观看 | 日韩在线| 日韩国产欧美一区 | 丝袜美腿一区二区三区动态图 | 成人av一区 | 一区二区在线 | 香蕉av免费 | 精品国产1区2区3区 一区二区手机在线 | 一区二区三区四区不卡视频 | 超碰97人人人人人蜜桃 | 精品久久99 | 日韩一区二区三区在线观看 | 黄色毛片免费 | 国产精久久久久久久 | 日韩在线播放av | 天天综合久久 | 一级毛片成人免费看a | 中文av在线播放 | 国产日韩一区二区 | 久久久久久亚洲精品 | 欧美亚洲视频 | 日韩欧美国产不卡 | 青青草久久| 九九热精品视频 | 久久综合成人精品亚洲另类欧美 | 欧美视频在线播放 | 国产98色在线 | 日韩 |