求SQL语句?----比较难

hetao0423 2010-05-18 11:17:45
现有一张表A,里面有字段id,name,shouji,字段。现在记录为

id name shouji
1 张三 123456
2 张三 234999
3 张三 88892929
4 李四 3333333
5 李四 99988
6 王五 3333

想实现的效果是:

id name shouji

1 张三 123456/234999/88892929
2 李四 3333333/99988
3 王五 3333


请高人指点
...全文
72 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jaydom 2010-05-18
  • 打赏
  • 举报
回复


select (RANK() over(order by name))id,name,stuff((select ('/'+cast(shouji as varchar(20)) )
from tb
where name=t.name for xml path('')),1,1,'')shouji
from tb t
group by name
dawugui 2010-05-18
  • 打赏
  • 举报
回复
--sql 2005
CREATE TABLE tb(id int,name nvarchar(10),shouji varchar(20))
INSERT INTO tb values(1,N'张三','123456')
INSERT INTO tb values(2,N'张三','234999')
INSERT INTO tb values(3,N'张三','88892929')
INSERT INTO tb values(4,N'李四','3333333')
INSERT INTO tb values(5,N'李四','99988')
INSERT INTO tb values(6,N'王五','3333')
go

select id = row_number() over(order by name), name, shouji = stuff((select ',' + shouji from tb t where name = tb.name for xml path('')) , 1 , 1 , '')
from tb
group by name

drop table tb

/*
id name shouji
-------------------- ---------- -------------------------
1 张三 123456,234999,88892929
2 李四 3333333,99988
3 王五 3333

(3 行受影响)
*/
yujunlin32167 2010-05-18
  • 打赏
  • 举报
回复
收藏学习一下...
dawugui 2010-05-18
  • 打赏
  • 举报
回复
CREATE TABLE tb(id int,name varchar(10),shouji varchar(20))
INSERT INTO tb values(1,'张三','123456')
INSERT INTO tb values(2,'张三','234999')
INSERT INTO tb values(3,'张三','88892929')
INSERT INTO tb values(4,'李四','3333333')
INSERT INTO tb values(5,'李四','99988')
INSERT INTO tb values(6,'王五','3333')
go

create function dbo.f_str(@name varchar(10)) returns varchar(1000)
as
begin
declare @str varchar(1000)
select @str = isnull(@str + ',' , '') + cast(shouji as varchar) from tb where name = @name
return @str
end
go

--调用函数
select id = (select count(1) from
(
select name , shouji = dbo.f_str(name) from tb group by name
) n where n.name < m.name) + 1 , m.* from
(
select name , shouji = dbo.f_str(name) from tb group by name
) m
drop function dbo.f_str
drop table tb

/*
id name shouji
----------- ---------- ---------------------
1 李四 3333333,99988
2 王五 3333
3 张三 123456,234999,88892929

(所影响的行数为 3 行)

*/
htl258_Tony 2010-05-18
  • 打赏
  • 举报
回复
----------------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-05-18 11:18:07
-- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
-- Blog : http://blog.csdn.net/htl258
----------------------------------------------------------------------------------

--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([id] [int],[name] [nvarchar](10),[shouji] [int])
INSERT INTO [tb]
SELECT '1','张三','123456' UNION ALL
SELECT '2','张三','234999' UNION ALL
SELECT '3','张三','88892929' UNION ALL
SELECT '4','李四','3333333' UNION ALL
SELECT '5','李四','99988' UNION ALL
SELECT '6','王五','3333'


-->SQL2000查询如下:
if object_id('f_getstr') is not null
drop function f_getstr
go
create function f_getstr(@name varchar(1000))
returns varchar(1000)
as
begin
declare @s varchar(1000)
select @s = isnull(@s + ',','') + ltrim(shouji) from tb where name = @name
return(@s)
end
go

select id=identity(int), name,shouji=dbo.f_getstr(name) into # from tb group by name

select * from #

drop table #

/*
id name shouji
-------------------- ---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 李四 3333333/99988
2 王五 3333
3 张三 123456/234999/88892929

(3 行受影响)
*/
黄_瓜 2010-05-18
  • 打赏
  • 举报
回复
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[name] varchar(4),[shouji] int)
insert [tb]
select 1,'张三',123456 union all
select 2,'张三',234999 union all
select 3,'张三',88892929 union all
select 4,'李四',3333333 union all
select 5,'李四',99988 union all
select 6,'王五',3333

--------------------------------查询开始------------------------------
--2005
;with t as
(
select min(id) as [id],
[name],
[shouji]=stuff((select '/'+ltrim([shouji]) from tb t where [name]=tb.[name] for xml path('')), 1, 1, '')
from tb
group by [name]
)
select Id=row_number() over(order by id),[name],[shouji] from t
/*

Id name shouji
-------------------- ---- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 张三 123456/234999/88892929
2 李四 3333333/99988
3 王五 3333

(3 行受影响)


*/
youyou2404 2010-05-18
  • 打赏
  • 举报
回复
--> 测试数据: [A]
if object_id('[A]') is not null drop table [A]
create table [A] (id int,name varchar(4),shouji nvarchar(20))
insert into [A]
select 1,'张三',123456 union all
select 2,'张三',234999 union all
select 3,'张三',88892929 union all
select 4,'李四',3333333 union all
select 5,'李四',99988 union all
select 6,'王五',3333

CREATE FUNCTION dbo.f_str(@id nvarchar(20))
RETURNS varchar(500)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + '/' + shouji FROM A WHERE name=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
select distinct name,dbo.f_str(name) t from a

name t
---- -----------------------------
李四 3333333/99988
王五 3333
张三 123456/234999/88892929

(3 行受影响)



drop function dbo.f_str
drop table a
永生天地 2010-05-18
  • 打赏
  • 举报
回复
蹭贴,蹭分
youyou2404 2010-05-18
  • 打赏
  • 举报
回复
--> 测试数据: [A]
if object_id('[A]') is not null drop table [A]
create table [A] (id int,name varchar(4),shouji nvarchar(20))
insert into [A]
select 1,'张三',123456 union all
select 2,'张三',234999 union all
select 3,'张三',88892929 union all
select 4,'李四',3333333 union all
select 5,'李四',99988 union all
select 6,'王五',3333

CREATE FUNCTION dbo.f_str(@id nvarchar(20))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + shouji FROM A WHERE name=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
select distinct name,dbo.f_str(name) t from a
name t
---- ------------------------------
李四 3333333,99988
王五 3333
张三 123456,234999,88892929

(3 行受影响)

drop function dbo.f_str
drop table a
黄_瓜 2010-05-18
  • 打赏
  • 举报
回复
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[name] varchar(4),[shouji] int)
insert [tb]
select 1,'张三',123456 union all
select 2,'张三',234999 union all
select 3,'张三',88892929 union all
select 4,'李四',3333333 union all
select 5,'李四',99988 union all
select 6,'王五',3333

--------------------------------查询开始------------------------------
--2005
select [name], [shouji]=stuff((select '/'+ltrim([shouji]) from tb t where [name]=tb.[name] for xml path('')), 1, 1, '')
from tb
group by [name]
/*

(6 行受影响)
name shouji
---- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
李四 3333333/99988
王五 3333
张三 123456/234999/88892929

(3 行受影响)


*/
dawugui 2010-05-18
  • 打赏
  • 举报
回复
/*
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-06
地点:广东深圳

描述:将如下形式的数据按id字段合并value字段。
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id value
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
*/
--1、sql2000中只能用自定义的函数解决
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

create function dbo.f_str(@id varchar(10)) returns varchar(1000)
as
begin
declare @str varchar(1000)
select @str = isnull(@str + ',' , '') + cast(value as varchar) from tb where id = @id
return @str
end
go

--调用函数
select id , value = dbo.f_str(id) from tb group by id

drop function dbo.f_str
drop table tb


--2、sql2005中的方法
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id

drop table tb


--3、使用游标合并数据
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
declare @t table(id int,value varchar(100))--定义结果集表变量
--定义游标并进行合并处理
declare my_cursor cursor local for
select id , value from tb
declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
open my_cursor
fetch my_cursor into @id , @value
select @id_old = @id , @s=''
while @@FETCH_STATUS = 0
begin
if @id = @id_old
select @s = @s + ',' + cast(@value as varchar)
else
begin
insert @t values(@id_old , stuff(@s,1,1,''))
select @s = ',' + cast(@value as varchar) , @id_old = @id
end
fetch my_cursor into @id , @value
END
insert @t values(@id_old , stuff(@s,1,1,''))
close my_cursor
deallocate my_cursor

select * from @t
drop table tb
htl258_Tony 2010-05-18
  • 打赏
  • 举报
回复
----------------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-05-18 11:18:07
-- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
-- Blog : http://blog.csdn.net/htl258
----------------------------------------------------------------------------------

--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([id] [int],[name] [nvarchar](10),[shouji] [int])
INSERT INTO [tb]
SELECT '1','张三','123456' UNION ALL
SELECT '2','张三','234999' UNION ALL
SELECT '3','张三','88892929' UNION ALL
SELECT '4','李四','3333333' UNION ALL
SELECT '5','李四','99988' UNION ALL
SELECT '6','王五','3333'


-->SQL查询如下:
SELECT id=row_number()over(order by getdate()),name,
shouji=stuff((select '/'+ltrim(shouji) from tb where name=t.name for xml path('')),1,1,'')
FROM [tb] t
GROUP BY name
/*
id name shouji
-------------------- ---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 李四 3333333/99988
2 王五 3333
3 张三 123456/234999/88892929

(3 行受影响)
*/
黄_瓜 2010-05-18
  • 打赏
  • 举报
回复
合并列值 
原著:邹建
改编:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-12-16 广东深圳

表结构,数据如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc

需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)

1. 旧的解决方法(在sql server 2000中只能用函数解决。)
--1. 创建处理函数
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value FROM tb WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO

-- 调用函数
SELECt id, value = dbo.f_str(id) FROM tb GROUP BY id

drop table tb
drop function dbo.f_str

/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/

--2、另外一种函数.
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

--创建一个合并的函数
create function f_hb(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct id ,dbo.f_hb(id) as value from tb

drop table tb
drop function dbo.f_hb

/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/

2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
-- 查询处理
SELECT * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM tb N
WHERE id = A.id
FOR XML AUTO
), ' <N value="', ','), '"/>', ''), 1, 1, '')
)N
drop table tb

/*
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc

(2 行受影响)
*/

--SQL2005中的方法2
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')
from tb
group by id

/*
id values
----------- --------------------
1 aa,bb
2 aaa,bbb,ccc

(2 row(s) affected)

*/

drop table tb

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧