<i id='EOfxH'><tr id='EOfxH'><dt id='EOfxH'><q id='EOfxH'><span id='EOfxH'><b id='EOfxH'><form id='EOfxH'><ins id='EOfxH'></ins><ul id='EOfxH'></ul><sub id='EOfxH'></sub></form><legend id='EOfxH'></legend><bdo id='EOfxH'><pre id='EOfxH'><center id='EOfxH'></center></pre></bdo></b><th id='EOfxH'></th></span></q></dt></tr></i><div id='EOfxH'><tfoot id='EOfxH'></tfoot><dl id='EOfxH'><fieldset id='EOfxH'></fieldset></dl></div>

<small id='EOfxH'></small><noframes id='EOfxH'>

<tfoot id='EOfxH'></tfoot>

    <legend id='EOfxH'><style id='EOfxH'><dir id='EOfxH'><q id='EOfxH'></q></dir></style></legend>
        • <bdo id='EOfxH'></bdo><ul id='EOfxH'></ul>

        SQL Server - 将行加入逗号分隔列表

        SQL Server - join rows into comma separated list(SQL Server - 将行加入逗号分隔列表)
      1. <tfoot id='Cm8uD'></tfoot>

      2. <legend id='Cm8uD'><style id='Cm8uD'><dir id='Cm8uD'><q id='Cm8uD'></q></dir></style></legend>
          <tbody id='Cm8uD'></tbody>
      3. <small id='Cm8uD'></small><noframes id='Cm8uD'>

          <bdo id='Cm8uD'></bdo><ul id='Cm8uD'></ul>
            <i id='Cm8uD'><tr id='Cm8uD'><dt id='Cm8uD'><q id='Cm8uD'><span id='Cm8uD'><b id='Cm8uD'><form id='Cm8uD'><ins id='Cm8uD'></ins><ul id='Cm8uD'></ul><sub id='Cm8uD'></sub></form><legend id='Cm8uD'></legend><bdo id='Cm8uD'><pre id='Cm8uD'><center id='Cm8uD'></center></pre></bdo></b><th id='Cm8uD'></th></span></q></dt></tr></i><div id='Cm8uD'><tfoot id='Cm8uD'></tfoot><dl id='Cm8uD'><fieldset id='Cm8uD'></fieldset></dl></div>

                1. 本文介绍了SQL Server - 将行加入逗号分隔列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有一个看起来像这样的临时表:

                  Let's suppose I have a temporary table which looks like this:

                  +----+------+
                  | Id | Value|
                  +----+------+    
                  | 1  |   1  |
                  | 1  |   2  |
                  | 1  |   3  |
                  | 2  |   1  |
                  | 2  |   2  |
                  +----+------+
                  

                  我希望我的桌子是这样的:

                  And I want my table to be like this:

                      +----+----------+
                      | Id | ValueList|
                      +----+----------+    
                      | 1  |   1,2,3  |
                      | 2  |   1,2    | 
                      +----+----------+
                  

                  所以基本上我需要将我的值分组为逗号分隔的列表.我已经尝试了以下方法:

                  So basically I need to group my values as a comma separated list. I already tried the following:

                  SELECT Id, STUFF((SELECT ',' + CAST(VALUE AS varchar) FROM @MyTable FOR XML PATH('')), 1 ,1, '') AS ValueList
                  FROM @MyTable
                  GROUP BY Id
                  

                  但我得到类似的信息:

                          +----+---------------------+
                          | Id |      ValueList      |
                          +----+---------------------+    
                          | 1  |   1,1,1,1,1,1,...   |
                          +----+---------------------+
                  

                  我找不到我做错了什么.有人可以帮忙解决这个查询吗?或者指出我正确的方向?谢谢.

                  I cant find what I am doing wrong. Could someone help with this query? Or point me to a right direction? Thank you.

                  推荐答案

                  您缺少子查询中的条件.

                  You are missing the condition inside the sub query.

                  SELECT t2.Id, STUFF((SELECT ',' + CAST(VALUE AS varchar) FROM @MyTable t1  where t1.Id =t2.ID FOR XML PATH('')), 1 ,1, '') AS ValueList
                  FROM @MyTable t2
                  GROUP BY t2.Id
                  

                  演示

                  这篇关于SQL Server - 将行加入逗号分隔列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                  1. <tfoot id='Yp2pG'></tfoot>

                        <legend id='Yp2pG'><style id='Yp2pG'><dir id='Yp2pG'><q id='Yp2pG'></q></dir></style></legend>
                          <tbody id='Yp2pG'></tbody>

                          <i id='Yp2pG'><tr id='Yp2pG'><dt id='Yp2pG'><q id='Yp2pG'><span id='Yp2pG'><b id='Yp2pG'><form id='Yp2pG'><ins id='Yp2pG'></ins><ul id='Yp2pG'></ul><sub id='Yp2pG'></sub></form><legend id='Yp2pG'></legend><bdo id='Yp2pG'><pre id='Yp2pG'><center id='Yp2pG'></center></pre></bdo></b><th id='Yp2pG'></th></span></q></dt></tr></i><div id='Yp2pG'><tfoot id='Yp2pG'></tfoot><dl id='Yp2pG'><fieldset id='Yp2pG'></fieldset></dl></div>
                        1. <small id='Yp2pG'></small><noframes id='Yp2pG'>

                            <bdo id='Yp2pG'></bdo><ul id='Yp2pG'></ul>