在SQL中通过查询显示组中的非聚合列 [英] showing non aggregate column in a group by query in SQL

查看:156
本文介绍了在SQL中通过查询显示组中的非聚合列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL 2008中有一个表

i have a table in SQL 2008 as


ID     Items
1      A
1      B
2      C
3      D
3      B

我希望得到的结果为


ID    Items
1     A,B
2     C
3     B,D

我使用过游标,但是它大大减慢了处理过程,我可以使用按组查询或其他任何方式来达到上述结果。

I have used cursors but it has considerably slowed the process , can i achieve the above result using group by query or through any other way.

感谢和问候

Kapil

推荐答案

您正在寻找一种连接结果的方法。我不认为MSSQL具有此功能,但是这里有一个很好的教程,介绍如何创建这样的方法: http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in- transact-sql /

You are looking for a way to concatenate the results. I don't think MSSQL has a function for that, but here's a very nice tutorial on how to create a method like that: http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

这篇关于在SQL中通过查询显示组中的非聚合列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆