对sql查询中的项目进行分组

发布于 2024-08-16 20:29:39 字数 621 浏览 3 评论 0 原文

我有一个表,其中包含类似(在sql server 2000中)的列,

MailCode   Mode   Name     Group
--------  -----  --------- -------
1          1      abc        0
1          1      def        0
1          1      qwe        1
2          2      aaw        0
2          2      aad        0

我想根据其余字段对名称字段进行分组,以便结果看起来像这样(应该只有一个唯一的mailCode、模式和组组合)

MailCode   Mode      Names            Group
---------   ------   ------------      -------
1           1        abc, def           0
1           1        qwe                1
2           2        aaw, aad           0

如何我为此创建了 sql 查询?

I have a table with columns like (in sql server 2000)

MailCode   Mode   Name     Group
--------  -----  --------- -------
1          1      abc        0
1          1      def        0
1          1      qwe        1
2          2      aaw        0
2          2      aad        0

I want to group the Name field based on the rest of the fileds so that the result looks like this (there should be only one unique mailCode, Mode and group combination)

MailCode   Mode      Names            Group
---------   ------   ------------      -------
1           1        abc, def           0
1           1        qwe                1
2           2        aaw, aad           0

How can I create the sql query for this?

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

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

发布评论

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

评论(3

最近可好 2024-08-23 20:29:39

我遇到了类似的问题,我必须在选择中连接一个字段,当时我的解决方案是创建一个返回结果的过程,并像这样调用它

select x as field1, y as field2, dbo.procedure as field3

I had a similar problem where I had to concatenate a field in the select, my solution at the time was to create a procedure that returned the result and called it like this

select x as field1, y as field2, dbo.procedure as field3

温柔少女心 2024-08-23 20:29:39

SQL Server 2000 解决方案

幸运的是,2000 年支持 COALESCE< /a>,因此您可以使用 COALESCE 技巧创建逗号分隔的值列表,在此链接中演示。由于变量的使用,您需要创建一个函数/过程并在主查询中调用它。基本上,只需将下面查询中的 STUFF() 替换为函数调用即可。

SQL Server 2005+ 解决方案:

  SELECT x.mailcode,
         x.mode,
         STUFF((SELECT y.name
                  FROM TABLE y
                 WHERE y.mailcode = x.mailcode
                   AND y.mode = x.mode
                   AND y.gropu = x.group
              GROUP BY y.mailcode, y.mode, y.group
               FOR XML PATH(', ')), 1, 1, '') AS name,
         x.group
    FROM TABLE x
GROUP BY x.mailcode, x.mode, x.group

SQL Server 2000 solution

Luckily, COALESCE is supported in 2000, so you can use the COALESCE trick to create a comma delimited list of values, demonstrated in this link. Because of the variable usage, you'll need to create a function/procedure and call it within the main query. Basically, just replace the STUFF() in the query below with the function call.

SQL Server 2005+ solution:

  SELECT x.mailcode,
         x.mode,
         STUFF((SELECT y.name
                  FROM TABLE y
                 WHERE y.mailcode = x.mailcode
                   AND y.mode = x.mode
                   AND y.gropu = x.group
              GROUP BY y.mailcode, y.mode, y.group
               FOR XML PATH(', ')), 1, 1, '') AS name,
         x.group
    FROM TABLE x
GROUP BY x.mailcode, x.mode, x.group
格子衫的從容 2024-08-23 20:29:39

我想不出一个简单的查询可以得到你正在寻找的结果,但是沿着这些思路的一些逻辑应该可以让你到达你想要的地方:

1) Loop through distinct MailCode, Mode, Group Rows
  A) select all names in group
   A.1) Loop through names
   A.2) Concatenate them together into temp variable
  B) insert all data (MailCode, Mode, Group, temp variable) into temp table

公平警告,SQL 中的循环往往会对性能产生巨大的影响。大型数据集。不幸的是我不知道更好的方法来做到这一点。

I can't think of a simple query that will get the result you're looking for, but some logic along these lines should get you where you want:

1) Loop through distinct MailCode, Mode, Group Rows
  A) select all names in group
   A.1) Loop through names
   A.2) Concatenate them together into temp variable
  B) insert all data (MailCode, Mode, Group, temp variable) into temp table

Fair waring, looping in SQL tends to have a huge performance hit when it comes to large datasets. I unfortunately don't know a better way to do it.

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