惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Securelist
O
OpenAI News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News and Events Feed by Topic
S
Security Affairs
SecWiki News
SecWiki News
Project Zero
Project Zero
L
Lohrmann on Cybersecurity
P
Proofpoint News Feed
P
Palo Alto Networks Blog
L
LINUX DO - 最新话题
H
Hacker News: Front Page
Recent Commits to openclaw:main
Recent Commits to openclaw:main
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
K
Kaspersky official blog
The GitHub Blog
The GitHub Blog
I
InfoQ
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
B
Blog
IT之家
IT之家
AWS News Blog
AWS News Blog
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
Spread Privacy
Spread Privacy
N
News and Events Feed by Topic
Security Latest
Security Latest
美团技术团队
C
Check Point Blog
WordPress大学
WordPress大学
T
Tenable Blog
S
Security @ Cisco Blogs
Last Week in AI
Last Week in AI
博客园 - 聂微东
月光博客
月光博客
博客园 - 【当耐特】
S
Schneier on Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Secure Thoughts
Schneier on Security
Schneier on Security
C
Cisco Blogs
Cyberwarzone
Cyberwarzone

博客园 - dadamoney

程序员创业-行业分析之区分易混淆的基本概念 MSSQl分布式查询(转) 产品经理的创新思维 PM、PD、UE与UI的区别 浏览器并发连接数 从LiveJournal后台发展看 大型网站系统架构以及性能优化方法(转) 使用开源软件,设计高性能可扩展互动网站 大型互联网网站架构心得之一:分(转) 大型互联网网站架构心得之二:并、换和其它(转) 高并发高流量网站架构(转) 转:Session服务器配置指南与使用经验 ZedGraph属性(转) 转:windows服务器配置 (转)SQL Server 假执行,预执行 转:操作必须使用一个可更新的查询 访问IIS元数据库失败解决方法(转) 转:安装IIS无法找到zClientm.exe文件的解决办法 FF Sofari Opera chrome IE7 IE8 透明设置 转:c++builder读写INI文件 - dadamoney - 博客园
(转)SQL 合并列值
dadamoney · 2009-12-11 · via 博客园 - dadamoney

-- =============================================================================
--
 標題: 合并列值
--
 整理: takako_mu
--
 时间: 2009-10-19
--
 地点: 昆山
--
 =============================================================================
/*

表結構如下:
id    value 
----- ------ 
1    aa 
1    bb 
2    aaa 
2    bbb 
2    ccc 

結果如下:
id    values 
------ ----------- 
1      aa,bb 
2      aaa,bbb,ccc 

*/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 ------------------------------------------------------------
--
1. sql2000舊方法:利用函數
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(@r11''
end
go
select id, value = dbo.f_str(id) from tb group by id
 
------------------------------------------------------------
--
2. sql2000舊方法:另一個函數
create function f_hb(@id int
returns varchar(8000
as 
begin 
  
declare @str varchar(8000
  
set @str = '' 
  
select @str = @str + ',' + cast(value as varcharfrom 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 ------------------------------------------------------------
--
3. sql2005:outer apply
select A.id,B.[newValues] from 
    (
select distinct id from tb)A 
    
outer apply
    (
        
select [newValues]= stuff(replace(replace(
                (
                    
select [value] from tb N
                    
where id = A.id
                    
for xml auto
                ), 
'<N value="'','), '"/>'''), 11'')
    )B
/*
注:    APPLY 是在一个查询的 FROM 子句中指定的新的关系运算符。
    它允许您对外部表的每一行调用表值函数,可选地使用外部表的列作为函数的参数。
    APPLY 运算符有两种形式:CROSS APPLY 和 OUTER APPLY。
    如果表值函数为其返回一个空集合的话,前者不返回外部表的行,而后者则返回一个 NULL 值的行而不是函数的列。
*/------------------------------------------------------------
--
4. sql2005:for xml path 
select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 11''
from tb 
group by id 

posted on 2009-12-11 15:02  dadamoney  阅读(495)  评论()    收藏  举报