SQL Server:使用GroupBy从表中选择/不带有聚合列

发布于 2025-02-10 13:21:50 字数 2982 浏览 1 评论 0原文

我有表A的列和行,

TableIDfeaturedcol3 col4col4coldatement
11AD42022-06-22 09:00:00
22BC52022-06-22 09:00:00
31AE62022-- 10:00:00
43BD72022-06-22 11:00:00
52BB82022-06-22 16:00:00

以下

需要在SQL Server中需要结果
06-22AD4,6
2BC5,8
3BD7

如果我运行以下查询:

select featureid,
       STRING_AGG(col3,',') as col3,
       STRING_AGG(col4,',') as col4 
from table_server 
group by feature_id;

我将获得以下结果,

featureidcol3col4
1AD,AE4,6
2BC,BD,BD5,8
3BD7

我应该如何更改我的询问是让Col3只有一个记录?

我有这样的澄清,无论是否可能吗?

我对SQL Server的知识为零,谁能帮助我?

I have the columns and rows for the table A,

tableidfeatureidcol3col4coldatetime
11AD42022-06-22 09:00:00
22BC52022-06-22 09:00:00
31AE62022-06-22 10:00:00
43BD72022-06-22 11:00:00
52BB82022-06-22 16:00:00

I need the following result in the SQL Server,

featureidcol3col4
1AD4,6
2BC5,8
3BD7

If I run the following query:

select featureid,
       STRING_AGG(col3,',') as col3,
       STRING_AGG(col4,',') as col4 
from table_server 
group by feature_id;

I am getting the following result,

featureidcol3col4
1AD,AE4,6
2BC,BD5,8
3BD7

How Should I change my query to get the Col3 to have only one record?

I have this clarification, whether this is possible or not?

I have zero knowledge with SQL Server, can anyone help me?

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

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

发布评论

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

评论(1

百思不得你姐 2025-02-17 13:21:50

似乎您需要min而不是String_agg

select
  s.featureid,
  MIN(s.col3, ',') as col3,
  STRING_AGG(s.col4, ',') as col4 
from table_server s
group by
  s.feature_id;

Seems like you need MIN instead of STRING_AGG

select
  s.featureid,
  MIN(s.col3, ',') as col3,
  STRING_AGG(s.col4, ',') as col4 
from table_server s
group by
  s.feature_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文