Oracle11g
如何获得每对行满足最小总和的汇总行?当我指定时where total_lot > N
,我排除了我想要保留的行。
我想回答这个数据问题:哪些 id 的 KEYS 的总手数大于 6 ,而 LOCK 的总手数大于 4?
with inventory_items as
(
select 1 as item_id, 'KEYS' as code, '1020' as sub_code, 4 as lot_size from dual union all
select 1 as item_id, 'KEYS' as code, '2210' as sub_code, 4 as lot_size from dual union all
select 1 as item_id, 'LOCK' as code, '1610' as sub_code, 3 as lot_size from dual union all
select 1 as item_id, 'LOCK' as code, '1031' as sub_code, 2 as lot_size from dual union all
select 2 as item_id, 'KEYS' as code, '1020' as sub_code, 2 as lot_size from dual union all
select 2 as item_id, 'KEYS' as code, '2210' as sub_code, 1 as lot_size from dual union all
select 2 as item_id, 'LOCK' as code, '1610' as sub_code, 1 as lot_size from dual union all
select 2 as item_id, 'LOCK' as code, '1031' as sub_code, 3 as lot_size from dual union all
select 3 as item_id, 'KEYS' as code, '1031' as sub_code, 8 as lot_size from dual
)
select distinct item_id, code,
sum(lot_size) over (partition by item_id, code) as total_lot
from inventory_items
order by item_id, code
期望的结果
- ID有LOCK和
- ID 有 KEYS 和
- KEYS 的 Total_lot 大于 6 并且
- LOCK 的 Total_lot 大于 4
输出:
ITEM_ID CODE TOTAL_LOT
----- ----- --------
1 KEYS 8
1 LOCK 5