0

enter image description here

Hi All, Please see attached image. Here I have a table "inv_stock" with "stockID" as primary key. I want to write a query to show stock quantity of each product with a column "Last vendor" using group by "invProductID" I tried this query

SELECT inv_stock.invProductID, inv_stock.vendorID, SUM( inv_stock.quantity ) AS quantity
FROM inv_stock
GROUP BY inv_stock.invProductID
ORDER BY inv_stock.stockID, inv_stock.created DESC

But instead showing last vendorID it shows first vendorID

enter image description here

Second image is the desired result i want from query Please help

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • his should not work. It probably does, because your `invProductID` is the primary key... Anyway you should add an aggregate function to your `vendorID` field! – Usagi Miyamoto Sep 08 '17 at 04:52
  • The latest vendor is defined as max(inv_stock.created) per invProductID or stockID (this is not clear from your question), hence the duplicate. – Shadow Sep 08 '17 at 05:40

0 Answers0