连接行 (sql server 2000)

发布于 2024-08-20 18:56:20 字数 357 浏览 7 评论 0原文

我在使用 SQL Server 2000 时遇到以下问题

下表大约有 200 行。

Company / Employee
1005 / A
1005 / B
1005 / C
1010 / X
1010 / Y
1020 / L
1020 / M
etc etc

我希望创建以下(逗号分隔)输出:

Company / Employees
1005 / A, B, C
1010 / X, Y
1020 / L, M
etc etc

我在 SQL Server 2000 中遇到了这个问题,而 2005 似乎提供了更简单的解决方案来解决这个问题!我希望有人有一个好主意来解释如何解决这个问题......

I have the following problem when using SQL Server 2000

The following table has around 200 rows.

Company / Employee
1005 / A
1005 / B
1005 / C
1010 / X
1010 / Y
1020 / L
1020 / M
etc etc

I wish to create the following (comma separated) output:

Company / Employees
1005 / A, B, C
1010 / X, Y
1020 / L, M
etc etc

I'm having a really hard time with this in SQL Server 2000, while 2005 seems to offer easier solutions to solve this issue! I hope someone has a bright idea to explain how to solve this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2024-08-27 18:56:20

我见过在 SQL 2000 中使用 UDF 的解决方案,但它们很可怕 - 它们基本上是为每个可能执行数百或数千次的组执行全新的 SELECT。实际上,使用游标会更好:

DECLARE @Temp TABLE
(
    Company int NOT NULL PRIMARY KEY,
    Employees varchar(4000) NOT NULL
)

DECLARE
    @LastCompany int,
    @Company int,
    @Employee varchar(100),
    @Employees varchar(4000)

DECLARE crEmployee CURSOR FAST_FORWARD FOR
    SELECT Company, Employee
    FROM @Tbl
    ORDER BY Company
OPEN crEmployee
FETCH NEXT FROM crEmployee INTO @Company, @Employee
WHILE (@@FETCH_STATUS = 0)
BEGIN
    IF (@LastCompany IS NULL)
        SET @Employees = @Employee
    ELSE IF (@Company = @LastCompany)
        SET @Employees = @Employees + ',' + @Employee
    ELSE BEGIN
        INSERT @Temp (Company, Employees)
        VALUES (@LastCompany, @Employees)

        SET @Employees = @Employee
    END
    SET @LastCompany = @Company
    FETCH NEXT FROM crEmployee INTO @Company, @Employee
END
CLOSE crEmployee
DEALLOCATE crEmployee

IF (@Employees IS NOT NULL)
    INSERT @Temp (Company, Employees)
    VALUES (@LastCompany, @Employees)

SELECT * FROM @Temp

有一个使用临时表和 UPDATE 的解决方案,类似于用于计算运行总计的解决方案,但它看起来很麻烦,并且在大多数情况下不会这样做性能比光标好得多。

如果有人真的非常想看到 UPDATE 版本,我会发布它,但我建议首先尝试光标版本,看看它是否“足够好”。

I've seen solutions using UDFs in SQL 2000, but they are scary - they're basically doing a brand-new SELECT for every group that might be executed hundreds or thousands of times. You're actually better off using a cursor:

DECLARE @Temp TABLE
(
    Company int NOT NULL PRIMARY KEY,
    Employees varchar(4000) NOT NULL
)

DECLARE
    @LastCompany int,
    @Company int,
    @Employee varchar(100),
    @Employees varchar(4000)

DECLARE crEmployee CURSOR FAST_FORWARD FOR
    SELECT Company, Employee
    FROM @Tbl
    ORDER BY Company
OPEN crEmployee
FETCH NEXT FROM crEmployee INTO @Company, @Employee
WHILE (@@FETCH_STATUS = 0)
BEGIN
    IF (@LastCompany IS NULL)
        SET @Employees = @Employee
    ELSE IF (@Company = @LastCompany)
        SET @Employees = @Employees + ',' + @Employee
    ELSE BEGIN
        INSERT @Temp (Company, Employees)
        VALUES (@LastCompany, @Employees)

        SET @Employees = @Employee
    END
    SET @LastCompany = @Company
    FETCH NEXT FROM crEmployee INTO @Company, @Employee
END
CLOSE crEmployee
DEALLOCATE crEmployee

IF (@Employees IS NOT NULL)
    INSERT @Temp (Company, Employees)
    VALUES (@LastCompany, @Employees)

SELECT * FROM @Temp

There's a solution using a temporary table and UPDATE similar to that used to compute running totals, but it's a beast to look at and in most cases won't do much better than the cursor for performance.

If anyone really desperately wants to see the UPDATE version, I'll post it, but I suggest trying the cursor version first and seeing if it's "good enough."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文