zoukankan      html  css  js  c++  java
  • T-SQL 合并多行数据显示到一行

    思路:

    自连接,使用For XML Path('')和STUFF函数

    SELECT * FROM STUDENT

    Name                      Team
    ------------------------- -------------------------
    Jack                      Team1
    Rose                      Team1
    Cindy                     Team1
    Richard                   Team2
    Jenny                     Team2

    SELECT
            T1.Team,
            STUFF((
                    SELECT 
                        CONCAT('/',T2.Name)
                    FROM
                            STUDENT T2
                    WHERE 
                            T2.Team=T1.Team
                    FOR XML PATH('')
                ),1,1,'')
            AS NAMES
    FROM 
            STUDENT T1
    GROUP BY 
            T1.Team 

    Team                      NAMES
    ------------------------- ---------------------------------------------------------

    Team1                     Jack/Rose/Cindy
    Team2                     Richard/Jenny

  • 相关阅读:
    P1541 乌龟棋
    P1725 琪露诺
    P1622 释放囚犯
    P1417 烹调方案
    积木大赛
    换教室
    C#文件和目录的操作
    C#应用程序所有已经打开的窗体的集合
    C#winform自定义滚动条
    C#ADO.NET基础二
  • 原文地址:https://www.cnblogs.com/wsion/p/5858247.html
Copyright © 2011-2022 走看看