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)

相关推荐
Мартин.7 分钟前
[CISSP] [5] 保护资产安全
数据库·安全·oracle
熠速8 分钟前
ITTIA DB Platform——实时嵌入式数据管理软件产品家族
数据库·嵌入式实时数据库
热爱编程的小曾40 分钟前
sqli-labs靶场 less 8
前端·数据库·less
THRUSTER111111 小时前
MySQL-- 函数(单行函数):数值函数, 字符串函数
数据库·mysql·函数·navicat·单行函数
橙序研工坊1 小时前
MySQL的进阶语法7(索引-B+Tree 、Hash、聚集索引 、二级索引(回表查询)、索引的使用及设计原则
数据库·sql·mysql
Bruce-li__1 小时前
深入理解Python asyncio:从入门到实战,掌握异步编程精髓
网络·数据库·python
小光学长2 小时前
基于vue框架的智能服务旅游管理系统54kd3(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
Bonnie_12152 小时前
07-MySQL-事务的隔离级别以及底层原理
数据库·mysql
ETLCloud数据集成社区2 小时前
ETLCloud是如何通过Oracle实现CDC的?
数据库·oracle·etl·实时数据同步
KATA~2 小时前
解决MyBatis-Plus枚举映射错误:No enum constant问题
java·数据库·mybatis