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

如何在 SQL2008R2 中將行整理為分隔字符串

How to collate rows to a delimited string in SQL2008R2(如何在 SQL2008R2 中將行整理為分隔字符串)
本文介紹了如何在 SQL2008R2 中將行整理為分隔字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我們目前正在升級我們用 C# 編寫的當前數據導入過程.

We are currently upgrading a current data import process we have written in C#.

作為升級過程的一部分,我們需要根據舊系統的結果檢查重寫導入過程的結果.

As part of the upgrade process, we need to check the results of the import process from the rewrite against the results of the old system.

我們所做的一項更改是將逗號分隔的列表分成另一個表中的行.這將使我們能夠使用簡單的連接過濾結果.

One of the changes we made was breaking comma-delimited lists into rows in another table. This will enable us to filter results using a simple join.

這是舊模式:

FormNumber      MainCategories
1               blue,green,red
2               yellow,red,blue
3               white

我們標準化為:

FormNumber      AttributeId      Value
1               1                blue
1               1                green
1               1                red
2               1                yellow
2               1                red
2               1                blue
3               1                white

現在,我們下一步是確認兩個過程的結果是否相同.其中一項檢查是將舊流程的 MainCategories 字段與規范化表的結果進行比較.

Now, our next step is to confirm that the results from the two processes are the same. One of these checks is to compare the MainCategories field of the old process with the results from the normalized tables.

最后,這將我們引向了一個問題:我如何創建一個以逗號分隔的新模式列表以與舊模式的值進行比較.

This leads us, finally, to the question: How do I create a comma-delimited list of the new schema to compare to the value of the old.

我們在這里嘗試了@Ritesh 提出的 XMLPath 解決方案:Concatenate多行合并成一個文本字符串?

We have tried the XMLPath solution proposed by @Ritesh here: Concatenate many rows into a single text string?

這里是改編的sql語句:

Here is the adapted sql statement:

Select distinct ST2.FormNumber, 
           (Select ST1.Value + ',' AS [text()]
            From cache.ArtifactAttribute ST1
            Where ST1.FormNumber= ST2.FormNumber
            ORDER BY ST1.FormNumber
            For XML PATH ('')) [Values]
     From cache.ArtifactAttribute ST2

問題是結果不正確.盡管 FormNumber 1 在表中只有三個條目,但 Values 列(動態構建的分隔字符串)顯示了不正確的結果.很明顯我們沒有正確實現sql代碼.

The problem is the results are not correct. Even though FormNumber 1 only has three entries in the table, the Values column (the dynamically built delimited string) shows incorrect results. Obviously we are not implementing the sql code correctly.

我們做錯了什么?

推薦答案

這里有一個方法供您嘗試:

Here is a way for you to try:

SELECT DISTINCT A.FormNumber, MainCategories
FROM YourTable A
CROSS APPLY (SELECT STUFF((SELECT ',' + Value 
                           FROM YourTable
                           WHERE FormNumber = A.FormNumber FOR XML PATH('')),1,1,'') MainCategories) B

盡管存在問題,您無法真正確定連接的項目的順序與您擁有的項目的順序相同,因為沒有一列明確給出該順序.這是一個使用此示例的有效 SQL Fiddle.

Though there is the problem where you can't really be sure that the order of the items concatenated is the same as the one you have, since there isn't a column that explictly gives that order. Here is a working SQL Fiddle with this example.

這篇關于如何在 SQL2008R2 中將行整理為分隔字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 久久国产精品久久久久久 | 免费看a| 久久天天 | 日韩在线一区二区 | 国产精品久久久久久久久久 | 天天玩夜夜操 | 午夜男人的天堂 | 九九精品在线 | 日韩中文字幕 | 四虎影院欧美 | 国产男人的天堂 | 欧美精品一区二区蜜桃 | 91亚洲精品在线 | 成人免费在线观看 | 99精品欧美一区二区三区综合在线 | 九九亚洲 | 免费黄色a视频 | 日韩综合在线 | 国产ts人妖系列高潮 | 亚洲综合一区二区三区 | 成人a视频 | 国产日韩欧美在线观看 | 亚洲三区在线观看 | av在线一区二区三区 | 欧美日韩国产一区二区三区 | 国产97人人超碰caoprom | 日韩一二区在线观看 | 精品免费av | 中文字幕亚洲区一区二 | 亚洲精品乱码8久久久久久日本 | 91久久| 国外成人在线视频网站 | 婷婷国产一区 | 日本成人在线免费视频 | 中文字幕一区二区三区乱码在线 | 亚洲精品久久久一区二区三区 | 国产成人免费视频网站高清观看视频 | 欧美成人久久 | 久久精品影视 | 免费av直接看 | 国内毛片毛片毛片毛片 |