SELECT APC.APC_NAME, APC.APC_ID, PTC.PTI_NAME, TEMP.PTI_ID, TEMP.COUNTS,ROW_NUMBER() OVER (PARTITION BY APC.APC_ID ORDER BY TEMP.PTI_ID) ROW_NUMBERFROM (SELECT APC_ID, NVL(INC_PTI_ID,8) PTI_ID, COUNT(*) COUNTS FROM (SELECT CCI.* FROM CX_INTEGRITY_CONTENT_INFO CCI WHERE PTC_ID = ‘1‘ AND INC_RTC_ID = ‘02‘) ...
1.ROW_NUMBER() OVER函数的基本用法语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)例如:row_number() OVER (PARTITION BYCOL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)如:不同的产品类别和计量单位下,查询所有企业的排名。SELECT product_type 产品类别,prickle 计量单位,production_name 企业名称,row_number() over(p...
select * from
(select prdt_no, bal from TEST_PIVOT where date_code >=‘20151121‘ and date_code<=‘20151123‘
)
pivot (sum(bal) as bal for prdt_no in (‘1‘ p1, ‘2‘ p2, ‘3‘ p3));
原文:http://www.cnblogs.com/xder/p/5152374.html
CREATE TABLE CC (Student NVARCHAR2(2),Course NVARCHAR2(2),Score INT ); INSERT into CC select N‘张三‘,N‘语文‘,78 from dual union all select N‘张三‘,N‘数学‘,87 from dual union all select N‘张三‘,N‘英语‘,82 from dual union all select N‘张三‘,N‘物理‘,90 from dual union all select N‘李四‘,N‘语文‘,65 from dual union all select N‘李四‘,N‘数学‘,77 from dual union all ...
1.使用条件查询 查询部门为20的员工列表 -- 查询部门为20的员工列表 SELECT t.DEPTNO,t.ENAME FROM SCOTT.EMP t where t.DEPTNO = ‘20‘ ; 效果:2.使用 listagg() WITHIN GROUP () 将多行合并成一行(比较常用) SELECT T .DEPTNO, listagg (T .ENAME, ‘,‘) WITHIN GROUP (ORDER BY T .ENAME) names FROM SCOTT.EMP T WHERE T .DEPTNO = ‘20‘ GROUP BY T .DEPTNO ...
select ‘四川省‘ nation ,‘成都市‘ city,‘第一‘ ranking from dual union all select ‘四川省‘ nation ,‘绵阳市‘ city,‘第二‘ ranking from dual union all select ‘四川省‘ nation ,‘德阳市‘ city,‘第三‘ ranking from dual union all select ‘四川省‘ nation ,‘宜宾市‘ city,‘第四‘ ranking from dual union all select ‘湖北省‘ nation ,‘武汉市‘ city,‘第一‘ ranking from dual union all selec...
*FROM (SELECT job,sum(decode(deptno,10,sal)) DEPT10,sum(decode(deptno,20,sal)) DEPT20,sum(decode(deptno,30,sal)) DEPT30,sum(decode(deptno,40,sal)) DEPT40FROM scott.empGROUP BY job)
ORDER BY 1;
JOB DEPT10 DEPT20 DEPT30 DEPT40
--------- ---------- ---------- ---------- ----------
ANALYST 6000
CLERK 1300 1900 950
MANAGER 2450 ...
?SQL Code?
12?select?concat(id,username)?str?from?app_userselect?id||username?str?from?app_user字符串转多列
实际上就是拆分字符串的问题,可以使用 substr、instr、regexp_substr函数方式
字符串转多行
使用union all函数等方式
wm_concat函数
首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行,接下来上例子,看看这个神奇的函数如何应用准备测试数据
?SQL Code?
123456?create...
转换过程如下:
1、创建表结构
CREATE TABLE RowToCol
( ID NUMBER(10) not null, USER_NAME VARCHAR2(20 CHAR), COURSE VARCHAR2(20 CHAR), SCORE VARCHAR2(20 CHAR)
)
2、插入测试数据(原始数据如图) 3、行转列实现效果 4、具体实现
主要原理是利用decode函数、聚集函数(sum/max/min/avg),结合group by分组实现的,具体sql如下:select t.user_name as 姓名,MAX(...
参考网址:http://blog.163.com/fushahui_1988@126/blog/static/82879994201192844355174/
一、多行转一列select id, vnum, to_char(wmsys.wm_concat(vname)) c from tab_test group by id,vnum;二、一列转多行with a as (select ‘/ABC/AA/AD/ABD/JI/CC/ALSKD/ALDKDJ‘ id from dual)select regexp_substr(id,‘[^/]+‘,1,rownum) id from aconnect by rownum <= length(regexp_replace(id,‘[^/]+‘))Oracle 行转列及列转行标签...
引:
为了举例说明,这里创建了一张成绩表,如下图所示:
比较:
1、sql中,这两个函数我们仅能使用case,代码及结果如下:
select name, case Subject when ‘语文‘ then 1 when ‘数学‘ then 2 when ‘英语‘ then 3 --else 3 end as ‘科目代码‘ from Results
同样的,我们可以用case实现行转列,代码及结果如下:
select Name, sum(...
table test( id varchar2(255) primary key not null, name varchar2(255), course varchar2(255), score varchar2(255) ); insert into test values(sys_guid(),‘zhangsan‘,‘语文‘,85); insert into test values(sys_guid(),‘zhangsan‘,‘数学‘,78); insert into test values(sys_guid(),‘zhangsan‘,‘英语‘,90); insert into test values(sys_guid(),‘lisi‘,‘语文‘,73); in...
行转列应该是数据库比较常见的操作了,在oracle中可以使用pivot、decode,可以参考呆瓜的blog:
http://blog.csdn.net/ch7543658/article/details/41146809SELECT name,MAX(DECODE(course, java, gread)) AS java,MAX(DECODE(course, c#, gread)) AS c#,MAX(DECODE(course, c, gread)) AS c,MAX(DECODE(course, sql, gread)) AS sql
FROM t
GROUP BY name;NAME JAVA C# C SQL
---------- ----...
多行转字符串
这个比较简单,用||或concat函数可以实现
select concat(id,username) str from app_userselect id||username str from app_user
字符串转多列
实际上就是拆分字符串的问题,可以使用 substr、instr、regexp_substr函数方式
字符串转多行
使用union all函数等方式
wm_concat函数
首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行,接下来上例子,看看这个神奇的函数如何...
drop table biz_bus_station_direct_0711;
--2.将站点数据等放入临时表
create table biz_bus_station_direct_0711 as
select ls.line_no line_no,bb.line_name line_name,t1.line_direct line_direct,s.station_id station_id,s.station_name station_name,t.point_num point_num_now,s.location_x x_now,s.location_y y_now,t1.point_num point_num_last,t1.point_x x_last,t1.point_y y_last,s.location_x - t1.point_x x_value...