Oracle: Oracle行转列、列转行的Sql语句总结
例子原型:
select bkg_num,shpr_cde from CD_XLS_UPLOAD_DETAIL where cd_xls_upload_uuid='' ;
运行结果如下:


一、多字段的拼接
将两个或者多个字段拼接成一个字段:
select bkg_num||shpr_cde from CD_XLS_UPLOAD_DETAIL where cd_xls_upload_uuid='' ;
运行结果:

二、行转列
将某个字段的多行结果,拼接成一个字段,获取拼接的字符串【默认逗号隔开】
select wm_concat(bkg_num) from CD_XLS_UPLOAD_DETAIL where cd_xls_upload_uuid='' ;
运行结果:
,
开拓:如果不想用逗号隔开,可以进行替换:
select replace(wm_concat(bkg_num),',','|') from test;
也可以进行分组的拼接:
select id,wm_concat(bkg_num) name from test group by id;
三、列转行
原图如下:

转成行的形式:

实现的sql:
create table demo(id int,name varchar(20),nums int); ---- 创建表
insert into demo values(1, 'apple', 1000);
insert into demo values(2, 'apple', 2000);
insert into demo values(3, 'apple', 4000);
insert into demo values(4, 'orange', 5000);
insert into demo values(5, 'orange', 3000);
insert into demo values(6, 'grape', 3500);
insert into demo values(7, 'mango', 4200);
insert into demo values(8, 'mango', 5500);
commit;
select name, sum(nums) from demo group by name;
select * from (select name, nums from demo) pivot(sum(nums) for name in ('apple','orange','grape','mango')); --实现sql
注意: pivot(聚合函数 for 列名 in(类型)) ,其中 in('') 中可以指定别名,in中还可以指定子查询,比如 select distinct code from customers
指定别名如:
select * from (select name, nums from demo) pivot(sum(nums) for name in ('apple' 苹果,'orange' 橘子,'grape' 葡萄,'mango' 芒果));
Oracle: Oracle行转列、列转行的Sql语句总结的更多相关文章
- oracle 多行转多列查询
oracle 多行转多列查询 ---create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int);inse ...
- Oracle 多行转多列
Oracle 多行转多列,列值转为列名 前段时间做调查问卷,客户创建自定义问卷内容,包括题目和选项内容; 之后需要到处问卷明细,,,,麻烦来咯 于是到网上到处搜索,没有直接结果;于是又找各种相似的 ...
- Oracle字符串行拆分成列的三种方式
Oracle字符串行拆分成列的三种方式 --muphy 开发过程中经常会遇到将前台多个值用逗号连接一同传递到后台查询,这个用逗号连接的字符串分隔的每个字符串分别对应Oracle数据库表的不同行. 如下 ...
- 问题:PLS-00204: 函数或伪列 'EXISTS' 只能在 SQL 语句中使用;结果:PL/SQL中不能用exists函数?
怎么写了一个语句带出这样的结果. 语句: if exists (select * from sysdatabases where name='omni') then 结果: ERROR 位于第 4 行 ...
- Mysql增加主键或者更改表的列为主键的sql语句
...
- Oracle行转列、列转行的Sql语句总结
多行转字符串 这个比较简单,用||或concat函数可以实现 SQL Code 12 select concat(id,username) str from app_userselect i ...
- Oracle行转列、列转行的Sql语句总结(转)
多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...
- Oracle 多行变一列的方法
多行变一列的方法有很多,觉得这个第一眼看懂了当时就用的这个办法. 情况是这样的.以下数据前几列是一样的,需要把VAT_VALUE_CHAR 的值放在同一行上. SELECT * FROM ps_vat ...
- oracle处理考勤时间,拆分考勤时间段的sql语句
最近一直在用mysql数据库做云项目,有段时间没有接触oracle了,昨天有朋友叫我帮忙用oracle处理一个考勤记录的需求,我在考虑如何尽量精简实现上面花了一些时间.于是把这个实现做个总结. 需求如 ...
- Oracle中查询和定位数据库问题的SQL语句
--1)查询和定位数据库问题的SQL语句--Oracle常用性能监控SQL语句.sql --1查询锁表信息 select vp.SPID, vs.P1, vs.P1RAW, vs.P2, vs.EVE ...
随机推荐
- 设置SharePoint2010列表的项目级权限
转:http://www.cfanz.cn/?c=article&a=read&id=24096 在SharePoint2010中我们经常会用到这样的权限设置,在一个列表中可以存储多个 ...
- Ubuntu全新系统一些配置
0.安装JDK,http://www.oracle.com/technetwork/java/javase/downloads/index.html 1.安装Intellij IDEA,https:/ ...
- Java项目中基于Hibernate分页总结
1,First of all, we should have a wrapper class for page,this class can calculate the startRow by th ...
- 深入设计模式(二)——单例模式(Singleton Pattern)
一.单例模式介绍 单例模式(Singleton Pattern),保证一个类只有一个实例,并提供一个访问它的全局访问点.单例模式因为Singleton封装它的唯一实例,它就可以严格地控制客户怎样访问它 ...
- Bzoj 1674: [Usaco2005]Part Acquisition dijkstra,堆
1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 337 Solved: 162[Sub ...
- use of undeclared identifier *** , did you mean ***. in xcode
A property is not the same thing os a instance variable, you should read a little bit of them, there ...
- openSuSE12.1 zypper LAMP
LAMP是由Apache MySQL PHP组成的,是在Linux下最受欢迎的软件组合之一,目前互联网上有很多网站运行在LAMP服务器上. Linux - 是富有情味的开源操作系统:Apache - ...
- UILabel字体加粗等属性和特效
/* Accessing the Text Attributes text property font property textColor property textAlignment pr ...
- [学习笔记]设计模式之Abstract Factory
写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 在上篇笔记Builder设计模式中,时の魔导士祭出了自己的WorldCreator.尽管它因此能创造出一个有山有树有房子的世界,但是白 ...
- Intellij 中的git操作 转!
http://blog.csdn.net/lovesummerforever/article/details/50032937 Git原理以后会分章节介绍,本次主要说一下intellij怎样操作git ...