??????ssql 语句

dadihongchang 2008-03-04 03:20:58
tb_A
NO Name
A001 aa
A003 bb
A002 cc
tb_B
Name paiming
aa 1
aa 9
bb 3
cc 2
cc 5
cc 7

得到的效果
Name paiming
aa 1,9
bb 2
cc 2,5,7
...全文
59 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dawugui 2008-03-04
--以下为2005中写法
create table tb_B(Name varchar(10), paiming int)
insert into tb_B values('aa', 1)
insert into tb_B values('aa', 9)
insert into tb_B values('bb', 3)
insert into tb_B values('cc', 2)
insert into tb_B values('cc', 5)
insert into tb_B values('cc', 7)
go

go
-- 查询处理
SELECT * FROM(SELECT DISTINCT name FROM tb_b)A OUTER APPLY(
SELECT [paiming]= STUFF(REPLACE(REPLACE(
(
SELECT paiming FROM tb_b N
WHERE name = A.name
FOR XML AUTO
), '<N paiming="', ','), '"/>', ''), 1, 1, '')
)N

drop table tb_B

/*
name paiming
---------- -------
aa 1,9
bb 3
cc 2,5,7
(3 行受影响)
*/
回复
dawugui 2008-03-04
--以下为2000中的写法,其中tb_A表没有用处.
create table tb_A(NO varchar(10), Name varchar(10))
insert into tb_A values('A001', 'aa')
insert into tb_A values('A003', 'bb')
insert into tb_A values('A002', 'cc')
create table tb_B(Name varchar(10), paiming int)
insert into tb_B values('aa', 1)
insert into tb_B values('aa', 9)
insert into tb_B values('bb', 3)
insert into tb_B values('cc', 2)
insert into tb_B values('cc', 5)
insert into tb_B values('cc', 7)
go
--创建一个合并的函数
create function f_hb(@Name varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(paiming as varchar) from tb_B where Name = @Name
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct Name ,dbo.f_hb(Name) as paiming from tb_B

drop table tb_A,tb_B
drop function f_hb

/*
Name paiming
---------- ----------
aa 1,9
bb 3
cc 2,5,7
(所影响的行数为 3 行)
*/
回复
QQQQAnnie 2008-03-04
create table tb2(code char(4),dep varchar(20))
insert tb2 select '0001','information'
insert tb2 select '0001','operate'
insert tb2 select '0002','home'
insert tb2 select '0003','code'
insert tb2 select '0003','office'

go
Create FUNCTION dbo.f_str(@code char(4))
RETURNS varchar(500)
AS
BEGIN
DECLARE @re varchar(500)
SET @re=''
SELECT @re=@re+','+dep
FROM tb2
WHERE code=@code
RETURN(STUFF(@re,1,1,''))
END
GO

select distinct a.name,dep=dbo.f_str(a.code)
from tb1 a,tb2 b
where a.code=b.code

drop table tb1,tb2
drop function f_str
回复
pt1314917 2008-03-04

create function wsp(@name varchar(50))
returns varchar(500)
as
begin
declare @sql varchar(500)
select @sql=isnull(@sql+',','')+ltrim(paiming) from tb_B where name=@name
return @sql
end

select distinct name,dbo.wsp(name)[paiming] from tb_B
回复
JiangHongTao 2008-03-04
纯手写,未测试,思路就是这样,有代码错误改改。
create function dbo.getstr(@name varchar(100))
returns varchar(8000)
as
begin
declare @s varchar(8000)
select @s = isnull(@s+',','')+paiming from tb_b where name = @name
return @s
end
go
select name,dbo.getstr(nmae) from tb_b group by name
回复
dadihongchang 2008-03-04
不好意思 ,得到的效果发错了

Name paiming
aa 1,9
bb 3
cc 2,5,7

bb 是3.。写错了
回复
dawugui 2008-03-04
合并列值
原著:邹建
改编:爱新觉罗.毓华 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 行受影响)
*/

回复
dawugui 2008-03-04
/*
带符号合并行列转换(爱新觉罗.毓华 2007-11-19于海南三亚)

有表tb,其数据如下:
a b
1 1
1 2
1 3
2 1
2 2
3 1
如何转换成如下结果:
a b
1 1,2,3
2 1,2
3 1
*/

create table tb
(
a int,
b int
)
insert into tb(a,b) values(1,1)
insert into tb(a,b) values(1,2)
insert into tb(a,b) values(1,3)
insert into tb(a,b) values(2,1)
insert into tb(a,b) values(2,2)
insert into tb(a,b) values(3,1)
go

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

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

drop table tb
drop function f_hb

/*
结果
a b
----------- ------
1 1,2,3
2 1,2
3 1

(所影响的行数为 3 行)
*/

----------------------------------------------------
/*
多个前列的合并
数据的原始状态如下:
ID PR CON OP SC
001 p c 差 6
001 p c 好 2
001 p c 一般 4
002 w e 差 8
002 w e 好 7
002 w e 一般 1
用SQL语句实现,变成如下的数据
ID PR CON OPS
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)
*/

create table tb
(
id varchar(10),
pr varchar(10),
con varchar(10),
op varchar(10),
sc int
)

insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '差', 6)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '好', 2)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '一般', 4)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '差', 8)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '好', 7)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '一般', 1)
go

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

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

drop table tb
drop function f_hb

/*
结果
id pr con ops
---------- ---------- ---------- ------------------
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)

(所影响的行数为 2 行)
*/

----------------------------------------------------
/*如何将一列中所有的值一行显示
数据源
a
b
c
d
e
结果
a,b,c,d,e
*/

create table tb(col varchar(20))
insert tb values ('a')
insert tb values ('b')
insert tb values ('c')
insert tb values ('d')
insert tb values ('e')
go

--方法一
declare @sql varchar(1000)
set @sql = ''
select @sql = @sql + t.col + ',' from (select col from tb) as t
set @sql='select result = ''' + @sql + ''''
exec(@sql)
/*
result
----------
a,b,c,d,e,
*/

--方法二
declare @output varchar(8000)
select @output = coalesce(@output + ',' , '') + col from tb
print @output
/*
a,b,c,d,e
*/

drop table tb

回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-03-04 03:20
社区公告
暂无公告