不知道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)