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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/434014.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android音量调节修改

前言 今日公司,安卓设备的音量显示不正常,让我来修复这个bug,现在已修复,做个博客,记录一下,以后碰到类似一下子就好解决。 Android音量调节相关 路径 frameworks\base\services\core\java\com\android…

学校“数据结构”课程Project—扩展功能(自主设计)

目录 一、设想功能描述 想法缘起 目标功能 二、问题抽象 三、算法设计和优化 1. 易想的朴素搜索 / dp 搜索想法 动态规划(dp)想法 2. 思考与优化 四、算法实现 五、结果示例 附:使用的地图API 一、设想功能描述 想法缘起 OSM 导出…

【JavaEE进阶】 #{}和${}

文章目录 🍃前言🌳#{}和${}使⽤🚩Interger类型的参数(基础数据类型)🎈使用#{}🎈使用${} 🚩String类型的参数使用🎈#{}使用🎈${} 🎍#{}和${}区别&a…

数据库之 基础概念、安装mysql、sql语句基础

数据库之 基础概念、安装mysql、sql语句基础 【一】存储数据的演变过程: 文件存储: 初始阶段随意存放数据到文件,格式任意。目录规范引入: 软件开发使用目录规范,限制数据位置,建立专门文件夹。本地数据存…

java版代码生成器

之前实现的JRT代码生成器是M版的,那么用户必须用M库才能有代码生成器的功能。为了提供给就是不用M库的用户使用,JRT再提供脚本版的java代码生成器,方便直接连关系库生成JRT的代码。 实现: import JRT.Core.MultiPlatform.JRTCon…

接口测试怎么测?接口测试的流程和步骤(超详细)

🍅 视频学习:文末有免费的配套视频可观看 🍅 点击文末小卡片 ,免费获取软件测试全套资料,资料在手,薪资嘎嘎涨 一、什么是接口测试 我们要想知道接口测试怎么做,首先要明白接口测试是什么?一般…

<蓝桥杯软件赛>零基础备赛20周--第18周--动态规划初步

报名明年4月蓝桥杯软件赛的同学们,如果你是大一零基础,目前懵懂中,不知该怎么办,可以看看本博客系列:备赛20周合集 20周的完整安排请点击:20周计划 每周发1个博客,共20周。 在QQ群上交流答疑&am…

HCS-华为云Stack-FusionSphere

HCS-华为云Stack-FusionSphere FusionSphere是华为面向多行业客户推出的云操作系统解决方案。 FusionSphere基于开放的OpenStack架构,并针对企业云计算数据中心场景进行设计和优化,提供了强大的虚拟化功能和资源池管理能力、丰富的云基础服务组件和工具…

栈和队列的动态实现(C语言实现)

✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅ ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ 🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿&#x1…

【LeetCode】112. 路径总和(简单)——代码随想录算法训练营Day18

题目链接:112. 路径总和 题目描述 给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则&…

ENVI下基于知识决策树提取地表覆盖信息

基于知识的决策树分类是基于遥感影像数据及其他空间数据,通过专家经验总结、简单的数学统计和归纳方法等,获得分类规则并进行遥感分类。分类规则易于理解,分类过程也符合人的认知过程,最大的特点是利用的多源数据。 决策树分类主要的工作是获取规则,本文介绍使用CART算法…

第17章_反射机制(理解Class类并获取Class实例,类的加载与ClassLoader的理解,反射的基本应用,读取注解信息,体会反射的动态性)

文章目录 第17章_反射机制本章专题与脉络1. 反射(Reflection)的概念1.1 反射的出现背景1.2 反射概述1.3 Java反射机制研究及应用1.4 反射相关的主要API1.5 反射的优缺点 2. 理解Class类并获取Class实例2.1 理解Class2.1.1 理论上2.1.2 内存结构上 2.2 获取Class类的实例(四种方…