java 实现行转列,Oracle中“行转列”的实现方式

在报表的开发当中,难免会遇到行转列的问题。

以Oracle中scott的emp为例,统计各职位的人员在各部门的人数分布情况,就可以用“行转列”:

scott的emp的原始数据为:

EMPNO

ENAME

JOB

MGR

HIREDATE

SAL

COMM

DEPTNO

7369

SMITH

CLERK

7902

12/17/1980

800.00

20

7499

ALLEN

SALESMAN

7698

2/20/1981

1600.00

300.00

30

7521

WARD

SALESMAN

7698

2/22/1981

1250.00

500.00

30

7566

JONES

MANAGER

7839

4/2/1981

2975.00

20

7654

MARTIN

SALESMAN

7698

9/28/1981

1250.00

1400.00

30

7698

BLAKE

MANAGER

7839

5/1/1981

2850.00

30

7782

CLARK

MANAGER

7839

6/9/1981

2450.00

10

7788

SCOTT

ANALYST

7566

4/19/1987

3000.00

20

7839

KING

PRESIDENT

11/17/1981

5000.00

10

7844

TURNER

SALESMAN

7698

9/8/1981

1500.00

0.00

30

7876

ADAMS

CLERK

7788

5/23/1987

1100.00

20

7900

JAMES

CLERK

7698

12/3/1981

950.00

30

7902

FORD

ANALYST

7566

12/3/1981

3000.00

20

7934

MILLER

CLERK

7782

1/23/1982

1300.00

10

使用“行转列”统计各职位的人员在各部门的分布人数后,数据为:

JOB

10(DEPTNO)

20(DEPTNO)

30(DEPTNO)

40(DEPTNO)

CLERK

1

2

1

0

SALESMAN

0

0

4

0

PRESIDENT

1

0

0

0

MANAGER

1

1

1

0

ANALYST

0

2

0

0

一、经典的实现方式

主要是利用decode函数、聚合函数(如max、sum等)、group by分组实现的

70f79da2e83d6db274a12166f63b31f8.png

1d717559d0dc31cf413685c6300fd1d3.png

select t.job, count(decode(t.deptno, '10', 1)) as "10(DEPTNO)",

count(decode(t.deptno, '20', 1)) as "20(DEPTNO)",

count(decode(t.deptno, '30', 1)) as "30(DEPTNO)",

count(decode(t.deptno, '40', 1)) as "40(DEPTNO)"

from scott.emp t

group by t.job;

Classical Approach

二、PIVOT

Oracle 11g后,出现PIVOT,更简便地实现“行转列”。使用前,需确定数据库环境大于11g,最好也确认下生产环境的数据库是否大于11g,避免项目后期出现状况。

1db7b87c84c57b96678c66d80f32ab6f.png

2032e82e8713449fe7ce956c7435c204.png

with tmp_tab as(

select t.job, t.deptno

from scott.emp t

)

select * from tmp_tab t pivot(count(1) for deptno in (10, 20, 30, 40));

PIVOT

三、PIVOT XML

使用经典的方法和PIVOT方法,DEPTNO的参数是硬编码的。而通过PIVOT XML能解决这一问题,使分列条件可以是动态的。但,输出的是XML的CLOB的格式。目前,Java读取PIVOT XML CLOB貌似比较困难(本人没有成功读取,可见下文描述,如有知晓者,请知悉)。

a0e70231e541fa6aaa66598eaf00af96.png

7ca9ea6298880c5282893341b7751848.png

with tmp_tab as(

select t.job, t.deptno

from scott.emp t

)

select * from tmp_tab t pivot xml (count(1) for deptno in (select deptno from scott.dept));

PIVOT XML

然而,当写完上面PIVOT XML滴时候,使用Java读取数据时,却发现读取不了PIVOT XML的CLOB(普通的并且数据相同的CLOB却能正常读取)

努力了几天,亦尝试下载目前最新的OJDBC,但仍然报错。

报错为

“Invalid column type: getCLOB not implemented for class oracle.jdbc.driver.T4CNamedTypeAccessor”–ojdbc6.jar

https://www.cnblogs.com/nick-huang/tag/Java/default.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现Oracle分页查询的示例代码: ```java import java.sql.*; public class OraclePagination { public static void main(String[] args) throws SQLException { // 定义分页参数 int pageNo = 1; // 页码 int pageSize = 10; // 每页记录数 // 连接数据库 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "username", "password"); PreparedStatement stmt = null; ResultSet rs = null; try { // 查询总记录数 String countSql = "SELECT COUNT(*) FROM table_name"; stmt = conn.prepareStatement(countSql); rs = stmt.executeQuery(); int totalRecord = 0; if (rs.next()) { totalRecord = rs.getInt(1); } rs.close(); stmt.close(); // 计算总页数 int totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1; // 查询当前页的记录 String dataSql = "SELECT * FROM (SELECT t.*, ROWNUM rn FROM (SELECT * FROM table_name ORDER BY id) t WHERE ROWNUM <= ?) WHERE rn >= ?"; stmt = conn.prepareStatement(dataSql); stmt.setInt(1, pageNo * pageSize); stmt.setInt(2, (pageNo - 1) * pageSize + 1); rs = stmt.executeQuery(); // 处理记录 while (rs.next()) { // 处理记录... } // 输出分页信息 System.out.println("总记录数:" + totalRecord); System.out.println("总页数:" + totalPage); System.out.println("当前页:" + pageNo); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源 if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } } } ``` 在以上示例代码,我们首先定义了分页参数,然后连接数据库,查询总记录数,并根据总记录数计算总页数。接着,我们使用SQL语句实现分页查询,该SQL语句使用了ROWNUM伪列,ROWNUM伪列是Oracle特有的一种伪列,它是一个按照顺序从1开始递增的整数值,可以用于限制结果集的行数。最后,我们通过循环遍历结果集,处理每条记录,并输出分页信息。最后,我们关闭了所有资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值