-3

I am having a Mysql table item_details as below and want to get the delta diff for same item from next row sale_count.

item_id,sale_count
10001,32
10001,43
10001,50
10005,34
10005,48
10007,82
10007,82

Need result as below

item_id,diff
10001,11
10001,7
10005,14
10007,0

Please help here

agarwal_achhnera
  • 2,582
  • 9
  • 55
  • 84
  • What have you tried so far? What are you stuck with? – Shadow Aug 04 '23 at 16:20
  • To calculate the delta difference for the same item from the next row's sale_count, you can use the LAG() function in MySQL. The LAG() function allows you to access the value of a given expression from a previous row. Here's the SQL query to achieve the desired result:`SELECT item_id, sale_count - LAG(sale_count) OVER (PARTITION BY item_id ORDER BY sale_count) AS diff FROM item_details;` – Divyesh pal Aug 04 '23 at 16:58

0 Answers0