【问题标题】:How to concatenate values from multiple columns in a single row如何在单行中连接多列的值
【发布时间】:2021-06-16 12:13:48
【问题描述】:

我有这张表,我需要将几列中的相同值连接到一行中。

RowId  ServerName  MyApplicationName   UserName       StartTime     LastTime     UsersCount
 1        Prod          MyProd           User1        2021/03/09     2021/03/09       10
 2        DEV1          MyApp1           User1        2021/03/12     2021/03/13       3
 3        DEV1          MyApp1           User1        2021/03/14     2021/03/15       3
 4        DEV1          MyApp1           User1        2021/03/16     2021/03/17       4
 5        DEV1          MyApp1           User1        2021/03/18     2021/03/19       5

我需要以下结果:

RowId  ServerName  MyApplicationName   UserName       StartTime     LastTime     UsersCount
 1        Prod          MyProd           User1        2021/03/09     2021/03/09       10
 2        DEV1          MyApp1           User1        2021/03/12     2021/03/19       15

我尝试了什么:

SELECT
       RowId,
       STUFF(
                        (SELECT ',' + ServerName
                        FROM tbl t1
                        WHERE t2.ServerName = t1.ServerName
                        FOR XML PATH(''))
                        , 1, 1, '')
                ) As ServerName,
       STUFF(
                        (SELECT ',' + MyApplicationName
                        FROM tbl t1
                        WHERE t2.MyApplicationName = t1.MyApplicationName
                        FOR XML PATH(''))
                        , 1, 1, '')
                ) As MyApplicationName,
       STUFF(
                        (SELECT ',' + UserName
                        FROM tbl t1
                        WHERE t2.UserName = t1.UserName
                        FOR XML PATH(''))
                        , 1, 1, '')
                ) As UserName,
       MIN(StartTime) AS StartTime,
       MAX(LastTime) AS LastTime,
       SUM(UserCount) AS UserCount

GROUP BY ApplicationName

但它不起作用。如何正确执行以及我做错了什么?

【问题讨论】:

  • 如果您认真使用 SQL Server 2008,是时候升级了!
  • @DaleK 我知道,但我们有一些 2008 年的服务器)你知道升级旧服务器有多难)

标签: sql sql-server tsql sql-server-2008


【解决方案1】:

这只是一个最小、最大、总和的练习,不是吗?

--DROP TABLE t 

CREATE TABLE t  (
    ServerName    varchar(100),
    myapplicationname   varchar(100),
    username    varchar(100),
    starttime   varchar(100),
    endtime    varchar(100),
    usercount int)

INSERT INTO  t ( ServerName, myapplicationname, username, starttime, endtime, usercount)
VALUES
('Prod','myprod','user1','2021/03/09','2021/03/09', 10),
('dev1','MyApp1','user1','2021/03/12','2021/03/13', 3),
('dev1','MyApp1','user1','2021/03/14','2021/03/15', 3),
('dev1','MyApp1','user1','2021/03/16','2021/03/17', 5)

select *
from t

select ServerName, myapplicationname, username, min(starttime), max(endtime), sum(usercount)
from t
group by ServerName, myapplicationname, username

结果:

【讨论】:

    猜你喜欢
    • 2013-10-27
    • 2014-10-13
    • 1970-01-01
    • 2017-09-25
    • 2022-08-14
    • 2017-02-21
    • 1970-01-01
    • 2021-12-16
    • 2021-06-15
    相关资源
    最近更新 更多