0

Oracle11g

行の各ペアが最小合計を満たす集計行を取得する方法は? 指定するwhere total_lot > Nと、保持したい行を除外します。

このデータに関する質問に答えたいと思います: 合計ロット サイズが 6 を超える KEYS を持ち、合計ロット サイズが 4 を超える LOCK を持つ ID はどれですか?

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         

望ましい結果

  1. ID には LOCK があり、
  2. ID には KEYS があり、
  3. KEYS の Total_lot が > 6 であり、
  4. LOCK の Total_lot は > 4 です

出力:

 ITEM_ID   CODE   TOTAL_LOT
 -----     -----  --------
 1         KEYS   8
 1         LOCK   5
4

2 に答える 2

0

このようなものをお探しですか?

select item_id, code, sum(lot_size)
from inventory_items ii
group by item_id, code
having sum(lot_size) > (case when code = 'KEYS' then 6
                             when code = 'LOCK' then 4
                        end)

ところで、サンプル コードのクエリは、group by.

あなたのコメントに応えて、2つのロットサイズの合計を意味すると仮定すると:

select item_id, sum(lot_size)
from inventory_items ii
group by item_id
having sum(case when code = 'KEYS' then lot_size else 0 end) > 6 and
       sum(case when code = 'LOCK' then lot_size else 0 end) > 4

値よりも大きいロットサイズを意味する場合は、句max()の代わりに使用します.sum()having

コードを含める最も簡単な方法は、元のデータをこれに結合することです。これを表現する 1 つの方法を次に示します。

select *
from inventory_items ii
where ii.item_id in (select item_id, sum(lot_size)
                     from inventory_items ii
                      group by item_id
                      having sum(case when code = 'KEYS' then lot_size else 0 end) > 6 and
                             sum(case when code = 'LOCK' then lot_size else 0 end) > 4
                    )
于 2013-02-28T00:20:00.707 に答える
0

これが解決策です。

説明

Gordon のクエリを使用して、数量要件を満たすすべての item_id を選択します。合計 KEYS > 6 であるため、このクエリは ID=3 を返します。

select item_id, 
       code, 
       sum(lot_size) total_lot
from inventory_items 
group by item_id, code
having sum(lot_size) > (case 
                         when code = 'KEYS' then 6
                         when code = 'LOCK' then 4
                        end)

出力

ITEM_ID  CODE  TOTAL_LOT
------------------------
 1       KEYS  8
 1       LOCK  5
 3       KEYS  8

ここで、ID=3 を除外する必要があります。これは、KEYS と LOCK の両方を持つ ID のみを含める必要があるためです。つまり、item_id ごとに正確に 2 行を返す必要があります。前のクエリを使用して、item_id の数をカウントする列を追加します。

select item_id,
       code,
       total_lot,
       count(*) over (partition by item_id) as item_count
from (
select item_id, 
       code, 
       sum(lot_size) total_lot
from inventory_items 
group by item_id, code
having sum(lot_size) > (case 
                         when code = 'KEYS' then 6
                         when code = 'LOCK' then 4
                        end)
 ) 

出力

ITEM_ID CODE  TOTAL_LOT ITEM_COUNT
------- ---- ---------- ----------
1       KEYS      8          2
1       LOCK      5          2
3       KEYS      8          1

これにより、行数をフィルタリングするための列が得られますが、それを使用するには、もう一度ネストする必要があります。

select item_id, 
       code,
       total_lot
from (
 select item_id,
       code,
       total_lot,
       count(*) over (partition by item_id) as item_count
from (
select item_id, 
       code, 
       sum(lot_size) total_lot
from inventory_items 
group by item_id, code
having sum(lot_size) > (case 
                         when code = 'KEYS' then 6
                         when code = 'LOCK' then 4
                        end)
 ) 
) where item_count=2

出力

 ITEM_ID  CODE  TOTAL_LOT
 ------------------------
 1       KEYS  8
 1       LOCK  5
于 2013-04-18T14:27:25.033 に答える