Oracle分栏(非分页)查询

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

数据源例子:

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

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

sql 复制代码
with t as (
    select '日用百货' as cat, '手电筒' as name, 20 as amount, '2024-01-27' as dt from dual
    union all
    select '餐饮美食' as cat, '鸡公煲' as name, 15.9 as amount, '2024-01-27' as dt from dual
    union all
    select '餐饮美食' as cat, '海带粉' as name, 6 as amount, '2024-01-27' as dt from dual
    union all
    select '日用百货' as cat, '垃圾桶' as name, 9.9 as amount, '2024-01-26' as dt from dual
    union all
    select '日用百货' as cat, '大铁锅' as name, 66 as amount, '2024-01-26' as dt from dual
    union all
    select '日用百货' as cat, '电火锅' as name, 216 as amount, '2024-01-26' as dt from dual
    union all
    select '日用百货' as cat, '电饭锅' as name, 166 as amount, '2024-01-26' as dt from dual
    union all
    select '餐饮美食' as cat, '老乡鸡' as name, 19.9 as amount, '2024-01-26' as dt from dual
    union all
    select '日用百货' as cat, '塑料小板凳' as name, 8 as amount, '2024-01-26' as dt from dual
    union all
    select '日用百货' as cat, '垃圾袋' as name, 7.5 as amount, '2024-01-26' as dt from dual
    union all
    select '日用百货' as cat, '塑料靠背凳' as name, 10 as amount, '2024-01-26' as dt from dual
    union all
    select '日用百货' as cat, '鞋刷' as name, 4 as amount, '2024-01-26' as dt from dual
    union all
    select '日用百货' as cat, '撑衣杆' as name, 8.5 as amount, '2024-01-26' as dt from dual
    union all
    select '餐饮美食' 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 md
    from (
         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
;

查询结果:

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

后面再找时间研究吧. (2024-01-27)

相关推荐
伏虎山真人14 分钟前
开源数据库 - mysql - mysql-server-8.4(gtid主主同步+ keepalived热切换)部署方案
数据库·mysql·开源
FIN技术铺3 小时前
Redis集群模式之Redis Sentinel vs. Redis Cluster
数据库·redis·sentinel
CodingBrother4 小时前
MySQL 中的 `IN`、`EXISTS` 区别与性能分析
数据库·mysql
代码小鑫5 小时前
A027-基于Spring Boot的农事管理系统
java·开发语言·数据库·spring boot·后端·毕业设计
小小不董5 小时前
Oracle OCP认证考试考点详解082系列16
linux·运维·服务器·数据库·oracle·dba
甄臻9245 小时前
Windows下mysql数据库备份策略
数据库·mysql
内蒙深海大鲨鱼5 小时前
qt之ui开发
数据库·qt·ui
不爱学习的YY酱5 小时前
【计网不挂科】计算机网络第一章< 概述 >习题库(含答案)
java·数据库·计算机网络
Mephisto.java5 小时前
【大数据学习 | HBASE高级】storeFile文件的合并
大数据·sql·oracle·json·hbase·database
这样の我5 小时前
hbase集成phoenix
大数据·数据库·hbase