简体   繁体   中英

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

i have 1 table inventory_movement here is data in table

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

i have another table inventory_orderproduct where i have cost date

 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

i want this result

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

here is my query

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

You can use the below query to get MAX cost for products

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 

For Retrieving the cost using the latest date use the below query

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

You can use below query it will pick the data according to the latest date

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 

I think this will help you: SQL: Select maximum value for each unique key? After you get a view with the max values for each ID you can do the regular inner join.

Here is the solution of your question.its working fine.if you like the answer please vote!

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 

In the given situation, this should work fine:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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