2 bb
1 cc
3 dd
2 ee
我想要得到这样的结果:1 aa,cc
2 bb,ee
3 dd
可以实现吗?请高手指点下.......非常感谢!!谢谢谢谢!!!
11 个解决方案
#1
--> 测试数据:@table
if object_id('tb') is not null drop table tb
create table tb ([id] int,[name] varchar(2))
insert into tb
select 1,'aa' union all
select 2,'bb' union all
select 1,'cc' union all
select 3,'dd' union all
select 2,'ee'
--查询
SELECT id,
name=CAST(MIN(name) as varchar)
+CASE
WHEN COUNT(*)=3 THEN ','
+CAST((SELECT name FROM tb WHERE id=a.id AND name NOT IN(MAX(a.name),MIN(a.name))) as varchar)
ELSE ''
END
+CASE
WHEN COUNT(*)>=2 THEN ','+CAST(MAX(name) as varchar)
ELSE ''
END
FROM tb a
GROUP BY id
DROP TABLE tb
--结果
----------------------------
1 aa,cc
2 bb,ee
3 dd
#2
--> 测试数据:tb
if object_id('tb') is not null drop table tb
create table tb ([id] int,[name] varchar(2))
insert into tb
select 1,'aa' union all
select 2,'bb' union all
select 1,'cc' union all
select 3,'dd' union all
select 2,'ee'
--创建函数
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(100)
AS
BEGIN
DECLARE @ret varchar(100)
SET @ret=''
SELECT @ret=@ret+','+name
FROM tb
WHERE id=@id
RETURN(STUFF(@ret,1,1,''))
END
--查询
SELECT id,name=dbo.f_str(id) FROM tb GROUP BY id
--结果
------------------------
1 aa,cc
2 bb,ee
3 dd
#4
select
id,
name=stuff((select ','+name from tb where id=t.id for xml path('')),1,1,'')
from
tb t
group by
id
#5
--> 测试数据:tb
if object_id('tb') is not null drop table tb
create table tb ([id] int,[name] varchar(20))
insert into tb
select 1,'aa' union all
select 2,'bb' union all
select 1,'cc' union all
select 3,'dd' union all
select 2,'ee'
SELECT id,name INTO #t FROM tb
ORDER BY id,name
DECLARE @id int,@name varchar(20)
UPDATE #t SET
@name=CASE WHEN @id=id THEN @name+','+name ELSE name END,
@id=id,
name=@name
SELECT id,name=MAX(name) FROM #t GROUP BY id
drop table #t
--结果
-----------------------------------
1 aa,cc
2 bb,ee
3 dd
#6
if object_id('tb') is not null drop table tb
create table tb ([id] int,[name] varchar(2))
insert into tb
select 1,'aa' union all
select 2,'bb' union all
select 1,'cc' union all
select 3,'dd' union all
select 2,'ee'
CREATE FUNCTION GET_STRING(@ID INT)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @NAME VARCHAR(500)
SELECT @NAME=ISNULL(@NAME+',','')+NAME FROM TB WHERE ID=@ID
RETURN @NAME
END
SELECT ID ,DBO.GET_STRING(ID)NAME FROM TB GROUP BY ID
ID NAME
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 aa,cc
2 bb,ee
3 dd
(所影响的行数为 3 行)
#7
--数据多了得用函数
declare @T table(userid int,username varchar(50),usercity varchar(50))
insert into @T (userid,username,usercity) values(1,'a','shangh')
insert into @T (userid,username,usercity) values(2,'b','beijing')
insert into @T (userid,username,usercity) values(3,'c','shangh')
insert into @T (userid,username,usercity) values(4,'d','beijing')
select usercity ,
case when count(1) = 2 then min(username) + ','+ max(username)
else min(username) end as username
from @T
group by usercity
-------------------------
beijing b,d
shangh a,c
#8
The easiest way is using function
#9
select Id,name=stuff((select ','+name from tb b where a.Id=b.Id for xml path('')),1,1,'')
from
tb a
group by Id
from
tb a
group by Id
#11
哇...楼上的都是高手.........好佩服啊!!问题解决啦!!发现SQL真的好强大!!要学的还有好多!!谢谢各位大虾!!结贴咯!!
#1
--> 测试数据:@table
if object_id('tb') is not null drop table tb
create table tb ([id] int,[name] varchar(2))
insert into tb
select 1,'aa' union all
select 2,'bb' union all
select 1,'cc' union all
select 3,'dd' union all
select 2,'ee'
--查询
SELECT id,
name=CAST(MIN(name) as varchar)
+CASE
WHEN COUNT(*)=3 THEN ','
+CAST((SELECT name FROM tb WHERE id=a.id AND name NOT IN(MAX(a.name),MIN(a.name))) as varchar)
ELSE ''
END
+CASE
WHEN COUNT(*)>=2 THEN ','+CAST(MAX(name) as varchar)
ELSE ''
END
FROM tb a
GROUP BY id
DROP TABLE tb
--结果
----------------------------
1 aa,cc
2 bb,ee
3 dd
#2
--> 测试数据:tb
if object_id('tb') is not null drop table tb
create table tb ([id] int,[name] varchar(2))
insert into tb
select 1,'aa' union all
select 2,'bb' union all
select 1,'cc' union all
select 3,'dd' union all
select 2,'ee'
--创建函数
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(100)
AS
BEGIN
DECLARE @ret varchar(100)
SET @ret=''
SELECT @ret=@ret+','+name
FROM tb
WHERE id=@id
RETURN(STUFF(@ret,1,1,''))
END
--查询
SELECT id,name=dbo.f_str(id) FROM tb GROUP BY id
--结果
------------------------
1 aa,cc
2 bb,ee
3 dd
#3
#4
select
id,
name=stuff((select ','+name from tb where id=t.id for xml path('')),1,1,'')
from
tb t
group by
id
#5
--> 测试数据:tb
if object_id('tb') is not null drop table tb
create table tb ([id] int,[name] varchar(20))
insert into tb
select 1,'aa' union all
select 2,'bb' union all
select 1,'cc' union all
select 3,'dd' union all
select 2,'ee'
SELECT id,name INTO #t FROM tb
ORDER BY id,name
DECLARE @id int,@name varchar(20)
UPDATE #t SET
@name=CASE WHEN @id=id THEN @name+','+name ELSE name END,
@id=id,
name=@name
SELECT id,name=MAX(name) FROM #t GROUP BY id
drop table #t
--结果
-----------------------------------
1 aa,cc
2 bb,ee
3 dd
#6
if object_id('tb') is not null drop table tb
create table tb ([id] int,[name] varchar(2))
insert into tb
select 1,'aa' union all
select 2,'bb' union all
select 1,'cc' union all
select 3,'dd' union all
select 2,'ee'
CREATE FUNCTION GET_STRING(@ID INT)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @NAME VARCHAR(500)
SELECT @NAME=ISNULL(@NAME+',','')+NAME FROM TB WHERE ID=@ID
RETURN @NAME
END
SELECT ID ,DBO.GET_STRING(ID)NAME FROM TB GROUP BY ID
ID NAME
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 aa,cc
2 bb,ee
3 dd
(所影响的行数为 3 行)
#7
--数据多了得用函数
declare @T table(userid int,username varchar(50),usercity varchar(50))
insert into @T (userid,username,usercity) values(1,'a','shangh')
insert into @T (userid,username,usercity) values(2,'b','beijing')
insert into @T (userid,username,usercity) values(3,'c','shangh')
insert into @T (userid,username,usercity) values(4,'d','beijing')
select usercity ,
case when count(1) = 2 then min(username) + ','+ max(username)
else min(username) end as username
from @T
group by usercity
-------------------------
beijing b,d
shangh a,c
#8
The easiest way is using function
#9
select Id,name=stuff((select ','+name from tb b where a.Id=b.Id for xml path('')),1,1,'')
from
tb a
group by Id
from
tb a
group by Id
#10
#11
哇...楼上的都是高手.........好佩服啊!!问题解决啦!!发现SQL真的好强大!!要学的还有好多!!谢谢各位大虾!!结贴咯!!