繁体   English   中英

如何使用一张表中的最新日期获取价值并加入另一张表

[英]how to get value using latest date from one table and joining to another table

我有 1 个表inventory_movement 这里是表中的数据

product_id | staff_name |  status | sum | reference_number
--------------------------------------------------
  1          zes             cp     1    000122
  2          shan             cp     4    000133

我有另一个表inventory_orderproduct,我有成本日期

 orderdate   product_id   cost
--------------------------------
01/11/2018    1            3200
01/11/2018    2             100
02/11/2018    1            4000
02/11/2018    1            500
03/11/2018    2            2000

我想要这个结果

product_id| staff_name | status | sum reference_number  |  cost
--------------------------------------------------------------
      1         zes        cp     1    000122         4000
      2         shan       cp     4    000133         2000

这是我的查询

select ipm.product_id, 
case when ipm.order_by_id is not null then 
(select au.first_name from users_staffuser us inner join auth_user au on us.user_id= au.id
where us.id = ipm.order_by_id) else '0' end as "Staff_name"
,ipm.status,
Sum(ipm.quantity), ip.reference_number
from inventory_productmovement ipm
inner join inventory_product ip on ipm.product_id = ip.id
inner join users_staffuser us on ip.branch_id = us.branch_id 
inner join auth_user au on us.user_id = au.id
AND ipm.status = 'CP'
group by ipm.product_id, au.first_name, ipm.status,  
ip.reference_number, ip.product_name
order by 1

您可以使用以下查询获取产品的最高费用

SELECT i.product_id,i.staff_name,i.status,i.sum reference_number ,s.MAXCost
FROM (SELECT product_id,MAX(cost) AS MAXCost
    FROM inventory_orderproduct 
    GROUP BY product_id ) s
JOIN inventory_movement i  ON i.product_id =s.product_id 

要使用最新日期检索成本,请使用以下查询

WITH cte as (
   SELECT product_id,cost
         ,ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY orderdate DESC) AS Rno
   FROM inventory_orderproduct )
 SELECT i.product_id,i.staff_name,i.status,i.sum reference_number ,s.Cost
 FROM cte s
 JOIN inventory_movement i  ON i.product_id =s.product_id 
 WHERE s.Rno=1

您可以在下面的查询中使用它将根据最新日期选择数据

WITH result as (
   SELECT product_id,cost
         ,ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY date DESC)
   FROM inventory_orderproduct )
 SELECT i.product_id,i.staff_name,i.status,i.sum reference_number ,s.Cost
 FROM result s
 JOIN inventory_movement i  ON i.product_id =s.product_id 

我认为这将对您有所帮助: SQL:为每个唯一键选择最大值? 在获得每个ID的最大值的视图之后,您可以进行常规的内部联接。

这是您问题的解决方案。它的工作正常。如果您喜欢答案,请投票!

SELECT i.product_id,i.staff_name,i.status,i.sum reference_number ,s.Cost
FROM (SELECT product_id,MAX(cost) AS Cost
    FROM inventory_orderproduct 
    GROUP BY product_id ) s
JOIN inventory_movement i  ON i.product_id =s.product_id 

在给定的情况下,这应该可以正常工作:

Select table1.product_id, table2.staff_name, table2.status, table2.reference_number, 
MAX(table1.cost)
FROM table2
LEFT JOIN table1 ON table1.product_id = table2.product_id
GROUP BY table2.product_id, table2.staff_name, table2.status, table2.reference_number

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM