当前位置:网站首页>oralce 处理列转行的三种方式 最后生成表格样式数据
oralce 处理列转行的三种方式 最后生成表格样式数据
2022-06-12 03:17:00 【紫薯馍馍】
【背景】
项目需要把数据库里面查询结果提取到表格里面来,区别日常的数据提取,本次数据表格样式复杂一些,因为涉及到收视人数、次数的问题,处理起来多费了一些时间,大概需求就是下面这样子

数据表的查询结果是下面这个样子:

看上去感觉很简单,但是之前没接触过,拿到这个需求是一头雾水的。
看看解决后的效果吧

【解决】
网上了解到了有三种解决方法,借用别人的例子,简单记录一下。
| name | subject | socre |
| 小米 | 语文 | 90 |
| 小米 | 数学 | 100 |
需要转换成:
| name | 语文 | 数学 |
| 小米 | 90 | 100 |
方法一 使用DECODE 函数,
这个方法plsql可以直接实现,但是我用jdbc无法实现,一直提示decode方法的计数方法有问题,不得不换一种,在解决过程中发现如果查询结果没有数据的时候,就会返回空,plsql这边没问题,但是在jdbc接收值的时候可能会报错,所以在查询的时候 每一列外面套用一个nvl()函数,可以解决这个返回值为空的问题。
SELECT
NAME,
SUM( DECODE(SUBJCT, "语文", SCORE) ) AS “语文”,
SUM( DECODE(SUBJCT, "数学", SCORE) ) AS “数学”
FROM
T_STUDENT_SOCRE
GROUP BY
NAME方法二、使用CASE WHEN
最终我是采用这个方式实现需求的,虽然列字段确实很多,但是一劳永逸还是可以试试
SELECT
NAME,
SUM( CASE SUBJECT WHEN "语文" THEN E_VALUE ELSE 0 END ) AS “语文”,
SUM( CASE SUBJECT WHEN "数学" THEN E_VALUE ELSE 0 END ) AS “数学”
FROM
T_STUDENT_SOCRE
GROUP BY
NAME方法三、使用的是PRVOT函数
这个方法我没使用成功,而且没有吃透他的处理方式,所以暂时就不多做叙述了
SELECT
NAME
"语文",
“数学”
FROM
T_STUDENT_SOCRE
PRVOT(MAX(SOCRE) for SUBJECT in ("语文" as “语文“,”数学“ as "数学") )最后我是通过WEB页面来实现这个需求的,因为手动导出还是比较麻烦,这里又回顾了一下,写table标签行合并 列合并的知识,差不多就是这个样子

边栏推荐
- Special materials | household appliances, white electricity, kitchen electricity
- Unity3d ugui translucent or linear gradient pictures display abnormally (blurred) problem solving (color space mismatch)
- The rise of another domestic mobile phone chip is close to the height reached by Huawei
- oracle之序列
- In 2022, don't you know the difference between arrow function and ordinary function?
- Summary -day11
- PHP life cycle and swoole life cycle
- Inverted string - two solutions
- Unity3D中DrawCall、Batches、SetPassCall
- Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
猜你喜欢

Demand and business model innovation - demand 9- prototype

Machine learning - dimensionality reduction (data compression, data visualization)

Redis gets the set of keys prefixed with XXX
![[Bank Research Report] technology enabled retail finance carbon neutral development report (2022) - download link attached](/img/bd/c1905dc601a598474ef37ea1c38f7f.jpg)
[Bank Research Report] technology enabled retail finance carbon neutral development report (2022) - download link attached
![[Business Research Report] 2021 global mobile game player white paper - download link attached](/img/6f/4425ead27fc3cf117f6756502477cf.jpg)
[Business Research Report] 2021 global mobile game player white paper - download link attached

Wechat applet project example - I have a paintbrush (painting)

The road of global evolution of vivo global mall -- multilingual solution

Kubernetes affinity learning notes

Requirements and business model innovation - Requirements 12 - process oriented modeling

ARD3M电动机保护器在煤炭行业中的应用
随机推荐
laravel 8 选用 jwt 进行接口验证
Introduce the functions of the new project aleo
微信小程序项目实例——体质计算器
安科瑞抗晃电产品在河北某化工项目的应用
Comparaison de la taille des fractions
[point cloud compression] variable image compression with a scale hyperprior
2020-12-12
Apache simple honeypot
Quelles sont les solutions dans tous les domaines?
golang的gin框架,各种接收参数的方式和各种绑定的区别?
利用ssh公钥传输文件
errno: -4078, code: ‘ECONNREFUSED‘, syscall: ‘connect‘, address: ‘127.0.0.1‘, port: 3306;postman报错
【鸿蒙】 使用定时器做一个简单的抢红包小游戏
string manipulation:
go 递归无限极分类
1187_ C language implementation of hysteresis processing
Kubernetes affinity learning notes
[Business Research Report] the salary growth rate report of each industry in 2022 includes regional growth rate - download link is attached
大整数的加与乘;
Selection (044) - what is the output of the following code?