如何使用自连接和分组依据编写查询?

发布于 2025-01-08 03:08:29 字数 870 浏览 4 评论 0原文

我有以下格式的 sql server 2008 db 表 FILE_DETAILS。

ID    FileName    Filesize_in_MB
--------------------------------
1     a.txt        5
2     b.txt        2
3     c.txt        2
3     d.txt        4
4     e.txt        6
4     f.txt        1
4     g.txt        2
5     h.txt        8
6     i.txt        7

现在我想要获取的内容如下

ID    FileName    Filesize_in_MB
--------------------------------
1     a.txt               5
2     b.txt               2
3     c.txt;d.txt         6
4     e.txt;f.txt;g.txt   9
5     h.txt               8
6     i.txt               7

在上面的结果中发生的情况 ID 成为唯一键,并且 FILENAME 已附加并由 ; 分隔,并且group by ID 总和中的 FILESIZE_IN_MB 字段

我尝试使用各种组合,例如 groupby + self join,还有子查询等等 但我想我错过了一些东西。

是否可以在 SQL 查询中处理这个问题?

提前致谢

I have sql server 2008 db table FILE_DETAILS in following format.

ID    FileName    Filesize_in_MB
--------------------------------
1     a.txt        5
2     b.txt        2
3     c.txt        2
3     d.txt        4
4     e.txt        6
4     f.txt        1
4     g.txt        2
5     h.txt        8
6     i.txt        7

now what i want to fetch is as bellow

ID    FileName    Filesize_in_MB
--------------------------------
1     a.txt               5
2     b.txt               2
3     c.txt;d.txt         6
4     e.txt;f.txt;g.txt   9
5     h.txt               8
6     i.txt               7

In above results what happens ID became unique key and FILENAME has get attached and separated by ; and also FILESIZE_IN_MB field in sum of group by ID

I tried with various combination like groupby + self join, also sub queries and all that
but i think i missing something.

is it possible to handle this in SQL query?

Thanks in advance

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

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

发布评论

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

评论(2

亚希 2025-01-15 03:08:29

试试这个:

SELECT  ID, 
        STUFF(( SELECT ';' + [FileName] 
                FROM FILE_DETAILS 
                WHERE ID = f.ID FOR XML PATH('')), 1, 1, ''), 
        SUM(Filesize_in_MB)
FROM    FILE_DETAILS f
GROUP BY ID

这里有更多信息:
将多行连接成一个文本字符串?

Try this:

SELECT  ID, 
        STUFF(( SELECT ';' + [FileName] 
                FROM FILE_DETAILS 
                WHERE ID = f.ID FOR XML PATH('')), 1, 1, ''), 
        SUM(Filesize_in_MB)
FROM    FILE_DETAILS f
GROUP BY ID

Here's some more information:
Concatenate many rows into a single text string?

强辩 2025-01-15 03:08:29

您应该能够使用 group by 来完成此操作。可以使用 sum 作为聚合器来聚合 Filesize_IN_MB。但是,要聚合 FileName,您可能需要创建一个 AGGREGATE在 SQL SERVER 2008R2 中。这将允许您使用Concatenate 作为聚合函数。

select Concatenate(FileName), sum(Filesize_IN_MB) FROM FILE_DETAILS group by ID

还有另一种聚合串联 看起来相当简单,但我没试过。

You should be able to do this using a group by. Aggregating Filesize_IN_MB can be done using sum as aggregator. However, to aggregate FileName you may need to create an AGGREGATE in SQL SERVER 2008R2. This will allow you to use Concatenate as an aggregation function.

select Concatenate(FileName), sum(Filesize_IN_MB) FROM FILE_DETAILS group by ID

There is another way of aggregate concatenation which seems fairly simple but I haven't tried.

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