字符串聚合函數(shù)(去除重復(fù)值)
更新時間:2009年06月06日 00:10:15 作者:
提供字符串的替代聚合函數(shù)
--功能:提供字符串的替代聚合函數(shù)
--說明:例如,將下列數(shù)據(jù)
--test_id test_value
--------------------
'a' '01,03,04'
'a' '02,04'
'b' '03,04,08'
'b' '06,08,09'
'c' '09'
'c' '10'
--轉(zhuǎn)換成test_vlaue列聚合后的函數(shù),且聚合后的字符串中的值不重復(fù)
--test_id test_value
--------------------
'a' '01,03,04,02'
'b' '03,04,08,06,09'
'c' '09,10'
--代碼-------------------------------------------GO
GO
if object_id(N'dbo.merge',N'FN') is not null
drop function dbo.merge
GO
--函數(shù)功能:字符串聚合及去除重復(fù)值
create function dbo.merge
(
@test_id varchar(50)
)
returns varchar(50)
as
begin
--字符串聚合-------------------------START
declare @s varchar(8000)
set @s = ''
select
@s = @s + test_value + ','
from test_a
where test_id = @test_id
--字符串聚合-------------------------END
--去除重復(fù)值-------------------------START
declare @value varchar(8000)--存儲第一個逗號前的值
declare @result varchar(8000)--存儲唯一值的中間字符串
set @result = ''
--有值的場合
while charindex(',',@s) <> 0
begin
--取第一個逗號前的值
set @value = left(@s,charindex(',',@s) -1)
--第一個逗號前的值沒在結(jié)果中出現(xiàn)
if charindex(',' + @value + ',',',' + @result) = 0
begin
--加入中間字符串
set @result = @result + @value + ','
end
--去除第一個值以及后面的逗號(剔除法),繼續(xù)循環(huán)判斷
set @s = right(@s,(len(@s) - charindex(',',@s)))
end
set @s = @result
--去除重復(fù)值-------------------------END
return left(@s,len(@s)-1)
end
GO
if object_id(N'test_a',N'U') is not null
drop table test_a
GO
create table test_a
(
test_id varchar(50),
test_value varchar(50)
)
insert into test_a
select 'a','01,03,04' union all
select 'a','02,04' union all
select 'b','03,04,08' union all
select 'b','06,08,09' union all
select 'c','09' union all
select 'c','10'
select
test_id,
test_value = dbo.merge(test_id)
from test_a
group by test_id
--說明:例如,將下列數(shù)據(jù)
--test_id test_value
--------------------
'a' '01,03,04'
'a' '02,04'
'b' '03,04,08'
'b' '06,08,09'
'c' '09'
'c' '10'
--轉(zhuǎn)換成test_vlaue列聚合后的函數(shù),且聚合后的字符串中的值不重復(fù)
--test_id test_value
--------------------
'a' '01,03,04,02'
'b' '03,04,08,06,09'
'c' '09,10'
--代碼-------------------------------------------GO
GO
if object_id(N'dbo.merge',N'FN') is not null
drop function dbo.merge
GO
--函數(shù)功能:字符串聚合及去除重復(fù)值
create function dbo.merge
(
@test_id varchar(50)
)
returns varchar(50)
as
begin
--字符串聚合-------------------------START
declare @s varchar(8000)
set @s = ''
select
@s = @s + test_value + ','
from test_a
where test_id = @test_id
--字符串聚合-------------------------END
--去除重復(fù)值-------------------------START
declare @value varchar(8000)--存儲第一個逗號前的值
declare @result varchar(8000)--存儲唯一值的中間字符串
set @result = ''
--有值的場合
while charindex(',',@s) <> 0
begin
--取第一個逗號前的值
set @value = left(@s,charindex(',',@s) -1)
--第一個逗號前的值沒在結(jié)果中出現(xiàn)
if charindex(',' + @value + ',',',' + @result) = 0
begin
--加入中間字符串
set @result = @result + @value + ','
end
--去除第一個值以及后面的逗號(剔除法),繼續(xù)循環(huán)判斷
set @s = right(@s,(len(@s) - charindex(',',@s)))
end
set @s = @result
--去除重復(fù)值-------------------------END
return left(@s,len(@s)-1)
end
GO
if object_id(N'test_a',N'U') is not null
drop table test_a
GO
create table test_a
(
test_id varchar(50),
test_value varchar(50)
)
insert into test_a
select 'a','01,03,04' union all
select 'a','02,04' union all
select 'b','03,04,08' union all
select 'b','06,08,09' union all
select 'c','09' union all
select 'c','10'
select
test_id,
test_value = dbo.merge(test_id)
from test_a
group by test_id
相關(guān)文章
關(guān)于SQLServer2005的學(xué)習(xí)筆記 XML的處理
在 SQLServer2005 中對 XML 的處理功能顯然增強(qiáng)了很多,提供了 query(),value(),exist(),modify(),nodes() 等函數(shù)。2010-04-04mdf文件和ldf文件導(dǎo)入到sql server 2005實現(xiàn)語句
導(dǎo)入mdf文件和ldf文件到數(shù)據(jù)庫是網(wǎng)站搬家或者是初學(xué)者學(xué)習(xí)源碼是必要的一步,接下來為大家詳細(xì)介紹實現(xiàn)sql語句,感興趣的你可不要錯過了哈希望可以幫助到你2013-03-03在登錄觸發(fā)器錯誤情況下連接SQL Server的方法
如果你創(chuàng)建了一個登錄觸發(fā)器,并且在這個觸發(fā)器中有一些不好的代碼,那么當(dāng)你嘗試著登錄時,你將會得到一個類似于圖一顯示的錯誤2011-07-07SQL server 2005將遠(yuǎn)程數(shù)據(jù)庫導(dǎo)入到本地的方法
這篇文章主要介紹了SQL server 2005將遠(yuǎn)程數(shù)據(jù)庫導(dǎo)入到本地的方法,需要的朋友可以參考下2015-01-01SQL Server 2005 鏡像構(gòu)建手冊(sql2005數(shù)據(jù)庫同步鏡像方案)
為了網(wǎng)站數(shù)據(jù)庫安全,我們需要備份數(shù)據(jù),這里為大家分享下sql2005數(shù)據(jù)庫同步鏡像方案,需要的朋友可以參考下2014-08-08SqlServer 2005 T-SQL Query 學(xué)習(xí)筆記(3)
利用ROW_NUMBER()進(jìn)行高效率的分頁。2010-02-02