来自同一表的MAX和SUM查询

时间:2017-06-12 21:09:54

标签: oracle sum max

我有一张类似下面的表格:

 part | location  | qty
    1 | Seattle   |  2
    2 | New York  |  3
    2 | New York  |  2
    1 | Seattle   |  1
    1 | New York  |  1
    1 | Warehouse |  5

这意味着我在西雅图有三部分,在纽约有一部分(此时忽略仓库)。如果我有一个位置,让我们说在达拉斯需要第1部分的库存,我希望西雅图发送它,因为他们有超过纽约。

SELECT part
      ,SUM(QTY_AVAILABLE) AS QTY_AVAILABLE
      ,LOCATION
FROM table1
WHERE LOCATION IN ('Warehouse','New York','Seattle','Dallas')
GROUP BY PN, LOCATION

上面给出了每个位置有多少物品。我想只显示上面列表中每个零件库存最多的位置。

我的问题的第二部分,我怎样才能显示转移零件的位置,并忽略仓库有零件的事实,除非仓库没有该位置?如果纽约用完第2部分,但是仓库有,那么查询应该说部件需要来自仓库。

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

这样的东西应该可以工作 - 所有这些都在一个查询中。请注意"测试数据中的其他示例" (我添加了第3和第4部分,每个部分都有不同的情况)。我没有过滤,所以唯一的地点是纽约,西雅图和达拉斯;如果需要,这很容易做到。

with
     test_data ( part, location, qty ) as (
       select 1, 'Seattle'  , 2 from dual union all
       select 2, 'New York' , 3 from dual union all
       select 2, 'New York' , 2 from dual union all
       select 1, 'Seattle'  , 1 from dual union all
       select 1, 'New York' , 1 from dual union all
       select 1, 'Warehouse', 5 from dual union all
       select 3, 'Dallas'   , 2 from dual union all
       select 3, 'Warehouse', 4 from dual union all
       select 4, 'Warehouse', 3 from dual
     )
--  End of test data (not part of the solution). Query begins BELOW THIS LINE.
select   part, location, sum_qty
from     (
           select   part, location, sum(qty) as sum_qty,
                    row_number() over ( partition by part 
                                        order by case when location != 'Warehouse' 
                                                      then 0 end,
                                                 sum(qty) desc
                                      ) as rn
           from     test_data
           group by part, location
         )
where    rn = 1
order by part
;

 PART LOCATION     SUM_QTY
----- --------- ----------
    1 Seattle            3
    2 New York           5
    3 Dallas             2
    4 Warehouse          3

答案 1 :(得分:0)

首先使用rank函数为给定部分选择TOP one,如下所示:

  select part, location from ( 
  select 
      row_number() over (order by sum(qty) desc) rn,
      part,
--      sum(qty) as qty_available,
      location
  from test
    where location in ('Warehouse','New York','Seattle','Dallas')
    group by part, location
    ) where rn = 1;
--    group by part, location

其次,对于非零库存的地点使用过滤器,并在New-York缺货时模拟选择具有第1部分的位置。

select part, location from ( 
  select 
      row_number() over (partition by part order by sum(qty) desc) rn,
      part,
      sum(qty) as qty_available,
      location
  from test
    where location in ('Warehouse',
    -- 'New York', -- New York ran out of the parts
    'Seattle','Dallas')
    group by part, location
    ) where rn = 1
    and part = 1
    and qty_available > 1;

我希望这会有任何好处。

答案 2 :(得分:0)

我认为以下内容也有效:

SELECT i.*
FROM inventory i
INNER JOIN (SELECT i2.part_no, MAX(i2.stock) AS STOCK
            FROM inventory i2
            WHERE i2.location <> 'Warehouse'
            GROUP BY i2.part_no) sub ON i.part_no = sub.part_no AND i.stock = sub.stock
WHERE i.location <> 'Warehouse'
UNION ALL
SELECT *
FROM inventory i
WHERE i.location = 'Warehouse'
AND NOT EXISTS (SELECT 'x'
                FROM inventory i2
                WHERE i2.part_no = i.part_no
                AND i2.location <> 'Warehouse');

对于问题的第一部分,您可以使用上半部分并删除有关位置的内容&lt;&gt; '仓库'