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

推荐订阅源

N
News and Events Feed by Topic
D
Docker
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
F
Full Disclosure
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
L
LangChain Blog
H
Help Net Security
B
Blog
T
Tailwind CSS Blog
V
V2EX
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
美团技术团队
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
I
InfoQ
Project Zero
Project Zero
I
Intezer
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Securelist
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
The Hacker News
The Hacker News

博客园 - calmzeal

微软更新导致的IIS7设置默认主页无效 【原创】Superset在windows下的安装配置 解决 Linux error while loading shared libraries: cannot open shared object file: No such file or directory 记录一次服务器CPU 100%的解决过程 Google Review中Zlib.Portable报错的一种排查解决方案 VS2010发布.NET2.0网站,出现“未预编译文件* 因此不能请求该文件”的解决办法 在 64 位版本的 Windows 上IIS ASP NET不支持JET4 问题 wse [Web 开发] 定制IE下载对话框的按钮(打开/保存) 【IE信息栏问题】本地html文件js被IE阻止的一些解决方法 分享一个js Tree - dTree SqlPlus Set常用设置 OleDB Transaction ORA-01000: maximum open cursors exceeded 超出打开游标的最大数 【转】神呀~~,给我个"本地数据库的替换方案"吧! 【CLR Via C#笔记】操作符重载 C#单件模式 【CLR Via C#笔记】 类型对象 【CLR Via C#笔记】 值类型与拆装箱、参数传递 [转] CSS完美兼容IE6/IE7/FF的通用方法
sql行转列
calmzeal · 2011-11-10 · via 博客园 - calmzeal

首先是Sql server下的用法:

一个表结构如下: id name contetID
  1 test 1 
  2 test1 1
  3 test2 1 
  4 test3 2
  5 test4 2  
把它的记录加入另一个表中,结构如下:

  contetID names
  1 test,test1,test2
  2 test3,test4

if OBJECT_ID('A','U')is not null drop table A
go
create table A 
(
id int, name nvarchar(10), contetID int
)
go
insert into A
select  1'test'1   union all
select  2'test1'1   union all
select 3'test2'1     union all
select 4'test3'2   union all
select 5'test4'2
go

select distinct contetID,
name=stuff((select ','+name from A as b where a.contetID=b.contetID for XML path('')),1,1,''
from A as a
/*
contetID    name
----------- ----------------
1           test,test1,test2
2           test3,test4
*/

--将上面的结果直接插入另一张表中即可

insert into othertb
select distinct contetID,
name=stuff((select ','+name from A as b where a.contetID=b.contetID for XML path('')),1,1,''
from A as a

核心函数:

1. select ','+name  for XML path('') :产生,test,test1,test2。 for XML path本来是用来生产xml格式数据的,详情见下:

http://www.cnblogs.com/doubleliang/archive/2011/07/06/2098775.html

2. stuff

Stuff(expression1_Str,startIndex,lengthInt,expression2_Str)函数共有四个参数,其功能是将expression1_Str中自startIndex位置起删除lengthInt个字符,然后将expression2插入到expression1_Str中的startIndex位置。

在此处用于去除xml path产生的字串中第一个,号。

然后,在Oracle中,没有for xml path,但是提供了更方便的函数:

函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行

http://blog.itpub.net/post/42245/522757

http://www.ningoo.net/html/2008/how_to_do_string_aggregate_on_oracle.html