SQL Server:使用GroupBy从表中选择/不带有聚合列
我有表A的列和行,
TableID | featured | col3 col4 | col4 | coldatement |
---|---|---|---|---|
1 | 1 | AD | 4 | 2022-06-22 09:00:00 |
2 | 2 | BC | 5 | 2022-06-22 09:00:00 |
3 | 1 | AE | 6 | 2022-- 10:00:00 |
4 | 3 | BD | 7 | 2022-06-22 11:00:00 |
5 | 2 | BB | 8 | 2022-06-22 16:00:00 |
以下
需要在SQL Server中需要 | 结果 | 我 |
---|---|---|
06-22 | AD | 4,6 |
2 | BC | 5,8 |
3 | BD | 7 |
如果我运行以下查询:
select featureid,
STRING_AGG(col3,',') as col3,
STRING_AGG(col4,',') as col4
from table_server
group by feature_id;
我将获得以下结果,
featureid | col3 | col4 |
---|---|---|
1 | AD,AE | 4,6 |
2 | BC,BD,BD | 5,8 |
3 | BD | 7 |
我应该如何更改我的询问是让Col3只有一个记录?
我有这样的澄清,无论是否可能吗?
我对SQL Server的知识为零,谁能帮助我?
I have the columns and rows for the table A,
tableid | featureid | col3 | col4 | coldatetime |
---|---|---|---|---|
1 | 1 | AD | 4 | 2022-06-22 09:00:00 |
2 | 2 | BC | 5 | 2022-06-22 09:00:00 |
3 | 1 | AE | 6 | 2022-06-22 10:00:00 |
4 | 3 | BD | 7 | 2022-06-22 11:00:00 |
5 | 2 | BB | 8 | 2022-06-22 16:00:00 |
I need the following result in the SQL Server,
featureid | col3 | col4 |
---|---|---|
1 | AD | 4,6 |
2 | BC | 5,8 |
3 | BD | 7 |
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,
featureid | col3 | col4 |
---|---|---|
1 | AD,AE | 4,6 |
2 | BC,BD | 5,8 |
3 | BD | 7 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您需要
min
而不是String_agg
Seems like you need
MIN
instead ofSTRING_AGG