簡體   English   中英

如何在postgres中計算兩列的乘積和並按兩列分組?

[英]How to do sum of product of two columns in postgres and group by two columns?

我有一個名為“庫存”的表,看起來像這樣:

日期,store_id,item_num,描述,現有商品,on_order,已售出,成本,價格

我想輸出如下內容:

date | store_id | inv costs| total on_hand
2014-08-03 | 100 | $57456 | 5876
2014-08-03 | 101 | $76532 | 4565
2014-07-27 | 100 | $12353 | 5346
2014-07-27 | 101 | $65732 | 8768

通過按日期分組並僅顯示商店ID為“ 100”和“ 101”。 庫存成本是我無法弄清的部分-我想找到那些日期那些商店的庫存成本,因此我想總結所有on_hand *成本。 任何幫助,將不勝感激,謝謝!

看來您快要到了:

  SELECT date,
         store_id,
         SUM(on_hand*cost) inv_costs,
         SUM(on_hand) total_on_hand
    FROM Inventory
   WHERE store_id IN (100,101)
GROUP BY date, 
         store_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM