Oracle分栏(非分页)查询

2024-01-28 03:52
文章标签 oracle 查询 分栏 非分

本文主要是介绍Oracle分栏(非分页)查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  不知道Oracle怎么进行数据分栏(分栏: 因数据列过长, 部分数据作为新列显示). 在这里先记录一下粗浅的查询方法.
数据源例子:

    select '日用百货' as cat, '手电筒' as name, 20 as amount, '2024-01-27' as dt from dualunion allselect '餐饮美食' as cat, '鸡公煲' as name, 15.9 as amount, '2024-01-27' as dt from dualunion allselect '餐饮美食' as cat, '海带粉' as name, 6 as amount, '2024-01-27' as dt from dualunion allselect '日用百货' as cat, '垃圾桶' as name, 9.9 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '大铁锅' as name, 66 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '电火锅' as name, 216 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '电饭锅' as name, 166 as amount, '2024-01-26' as dt from dualunion allselect '餐饮美食' as cat, '老乡鸡' as name, 19.9 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '塑料小板凳' as name, 8 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '垃圾袋' as name, 7.5 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '塑料靠背凳' as name, 10 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '鞋刷' as name, 4 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '撑衣杆' as name, 8.5 as amount, '2024-01-26' as dt from dualunion allselect '餐饮美食' as cat, '海带粉' as name, 6 as amount, '2024-01-26' as dt from dual

  思路: 创造提取新列的条件, 然后进行关联查询

with t as (select '日用百货' as cat, '手电筒' as name, 20 as amount, '2024-01-27' as dt from dualunion allselect '餐饮美食' as cat, '鸡公煲' as name, 15.9 as amount, '2024-01-27' as dt from dualunion allselect '餐饮美食' as cat, '海带粉' as name, 6 as amount, '2024-01-27' as dt from dualunion allselect '日用百货' as cat, '垃圾桶' as name, 9.9 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '大铁锅' as name, 66 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '电火锅' as name, 216 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '电饭锅' as name, 166 as amount, '2024-01-26' as dt from dualunion allselect '餐饮美食' as cat, '老乡鸡' as name, 19.9 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '塑料小板凳' as name, 8 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '垃圾袋' as name, 7.5 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '塑料靠背凳' as name, 10 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '鞋刷' as name, 4 as amount, '2024-01-26' as dt from dualunion allselect '日用百货' as cat, '撑衣杆' as name, 8.5 as amount, '2024-01-26' as dt from dualunion allselect '餐饮美食' as cat, '海带粉' as name, 6 as amount, '2024-01-26' as dt from dual
)
, tmd as (select p.*, ceil(p.rn/3) as dv, mod(p.rn, 3) as mdfrom (select row_number() over (partition by cat order by dt,amount desc) as rn, t.*from t) p
)
select t1.cat, t1.name as name1,t1.amount as amount1, t2.name as name2,t2.amount as amount2, t3.name as name3,t3.amount as amount3
from (select * from tmd where md=1) t1
left join (select * from tmd where md=2) t2 on t1.cat=t2.cat and t1.dv=t2.dv
left join (select * from tmd where md=0) t3 on t1.cat=t3.cat and t1.dv=t3.dv
order by t1.cat,t1.dv,t1.md
;

  查询结果:

CATNAME1AMOUNT1NAME2AMOUNT2NAME3AMOUNT3
日用百货电火锅216电饭锅166大铁锅66
日用百货塑料靠背凳10垃圾桶9.9撑衣杆8.5
日用百货塑料小板凳8垃圾袋7.5鞋刷4
日用百货手电筒20NULLNULLNULLNULL
餐饮美食老乡鸡19.9海带粉6鸡公煲15.9
餐饮美食海带粉6NULLNULLNULLNULL

在这里插入图片描述
后面再找时间研究吧. (2024-01-27)

这篇关于Oracle分栏(非分页)查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/652389

相关文章

oracle数据库索引失效的问题及解决

《oracle数据库索引失效的问题及解决》本文总结了在Oracle数据库中索引失效的一些常见场景,包括使用isnull、isnotnull、!=、、、函数处理、like前置%查询以及范围索引和等值索引... 目录oracle数据库索引失效问题场景环境索引失效情况及验证结论一结论二结论三结论四结论五总结ora

Oracle Expdp按条件导出指定表数据的方法实例

《OracleExpdp按条件导出指定表数据的方法实例》:本文主要介绍Oracle的expdp数据泵方式导出特定机构和时间范围的数据,并通过parfile文件进行条件限制和配置,文中通过代码介绍... 目录1.场景描述 2.方案分析3.实验验证 3.1 parfile文件3.2 expdp命令导出4.总结

Redis KEYS查询大批量数据替代方案

《RedisKEYS查询大批量数据替代方案》在使用Redis时,KEYS命令虽然简单直接,但其全表扫描的特性在处理大规模数据时会导致性能问题,甚至可能阻塞Redis服务,本文将介绍SCAN命令、有序... 目录前言KEYS命令问题背景替代方案1.使用 SCAN 命令2. 使用有序集合(Sorted Set)

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核

Oracle数据库执行计划的查看与分析技巧

《Oracle数据库执行计划的查看与分析技巧》在Oracle数据库中,执行计划能够帮助我们深入了解SQL语句在数据库内部的执行细节,进而优化查询性能、提升系统效率,执行计划是Oracle数据库优化器为... 目录一、什么是执行计划二、查看执行计划的方法(一)使用 EXPLAIN PLAN 命令(二)通过 S

PostgreSQL如何查询表结构和索引信息

《PostgreSQL如何查询表结构和索引信息》文章介绍了在PostgreSQL中查询表结构和索引信息的几种方法,包括使用`d`元命令、系统数据字典查询以及使用可视化工具DBeaver... 目录前言使用\d元命令查看表字段信息和索引信息通过系统数据字典查询表结构通过系统数据字典查询索引信息查询所有的表名可

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

ural 1026. Questions and Answers 查询

1026. Questions and Answers Time limit: 2.0 second Memory limit: 64 MB Background The database of the Pentagon contains a top-secret information. We don’t know what the information is — you

Mybatis中的like查询

<if test="templateName != null and templateName != ''">AND template_name LIKE CONCAT('%',#{templateName,jdbcType=VARCHAR},'%')</if>

Oracle type (自定义类型的使用)

oracle - type   type定义: oracle中自定义数据类型 oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式, 如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型 格式 :create or repla