id test Rank
1 hello 1
1 i 2
1 am 3
1 king 4
2 i 1
2 am 2
2 the 3
2 best 4一句sql查询如下结果:
id test
1 hello i am king
1 i am the best
1 hello 1
1 i 2
1 am 3
1 king 4
2 i 1
2 am 2
2 the 3
2 best 4一句sql查询如下结果:
id test
1 hello i am king
1 i am the best
解决方案 »
- 左侧工具栏效果
- 一个每秒都要循环运行的线程,如何让它不要占用cpu的时间
- 编码显示很多textbox,并将内容读入数组
- HTTP/1.1 500 Internal Server error
- 请问:如何实现系统自动编码
- 新手急用问题,我在form1里定义了一个int a 和一个int b .我想把a,b 的两个数值在form2中也能用.请问如何解决,高手们帮帮忙好吗?小弟在线
- 急求axSpreadsheet1用法
- 这样做进度条行不行
- 已经是第三个 100分了。我的问题居然还是没有说的明白细致阿。
- 远程请求api接口报错,本地测试正常,上传到服务器就报错。
- 正则表达式怎样判断连接中是否有.rar等
- 请问ComponentOne.Studio是什么来的?
=======================================
id test
1 hello i am king
2 i am the best强烈歉意
select distinct id,'i am the best' from table where id = 2
--测试表
create table Test
(id varchar(10),
Test varchar(10),
rank int)
--插入数据
insert into Test
select '1' ,'hello',1
union
select '1' ,'i' ,2
union
select '1' ,'am' ,3
union
select '1' ,'king' ,4
union
select '2' ,'i' ,1
union
select '2' ,'m' ,2
union
select '2' ,'the' ,3
union
select '2' ,'best' ,4
--一条动态SQL语句go
--合并函数
CREATE FUNCTION MergeCharField(@id varchar(255))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r=''
SELECT @r=@r+' '+rtrim(Test) FROM Test WHERE id=@id
order by rank
RETURN(@r)
END
GO
--调用
select id,dbo.MergeCharField(id)as text from test group by id
--删除测试环境
drop table test
drop FUNCTION MergeCharField/*
1 hello i am king
2 i m the best*/
from(
select distinct
id
from Test
)a
outer apply(
select
test= stuff(REPLACE(REPLACE(
(
SELECT test FROM test n
where id = A.id order by rank
for xml auto
), '<n test="', ' '), '"/>', ''), 1, 1, ''))n
============================
谁还能抛出一条sql2000下的
returns varchar(4000)
as
begin
declare @v varchar(400),@s varchar(400)
set @v=''select @v=@v+test from **
where id=@i
set @v=left(@v,len(@v)-1)
return @v
end
---------------------------
sql 2k 好像没有,需要套一个函数sql 2k5 目前不清楚昨天刚回答了一个同样的需求:
http://community.csdn.net/Expert/TopicView3.asp?id=5603231