0

Inventory Table:

enter image description here

Inventory History Table:

enter image description here

The query:

INSERT INTO inventory_history (SKU, Quantity, timestamp)
SELECT SKU, Quantity, modifiedtime FROM inventory WHERE modifiedtime BETWEEN '2016-12-25 00:00:00' AND '2016-12-26 00:00:00';

The Trigger:

CREATE TRIGGER `sold_diff` BEFORE INSERT ON `inventory_history`
FOR EACH ROW begin
declare prev_quantity int(11) default 0;
declare prev_sku varchar(255) default null;

select sku 
into prev_sku
from inventory_history
where prev_sku = NEW.sku
order by id desc
limit 1;

select quantity
into prev_quantity 
from inventory_history
order by id desc
limit 1;

set NEW.sold = prev_quantity
;
end

The Result:

enter image description here

Now, how it's set-up is it's taking prev_quantity from the previous row, and putting it into the sold column.

I can not figure out a way to bind SKU in with prev_quantity, so that it will give me the previous Quantity value from the corresponding SKU.

Desired Result:

enter image description here

I've messed with a variety of different WHERE clauses on the two declared, but nothing is working right.. so I'm thinking this is not the right path to take.

How can this be achieved?

bbruman
  • 667
  • 4
  • 20
  • Tell us about 4, 23, 57, & 58 – Strawberry Jan 01 '17 at 23:25
  • that's just me doing testing on my localhost and deleting columns so I can try my query again, thats why they are so weird. Normally it'd just be a normal autoincrement – bbruman Jan 01 '17 at 23:26
  • If it was me, I'd start over, with a proper a MCVE and a desired result - so none of this trigger business; that can come later, once you have a working query. – Strawberry Jan 01 '17 at 23:29
  • As stated by Gordon, you may be taking the wrong approach. Normally you would update your values in the inventory table, and that would then update (or LOG) that change into your history table. That being said create a procedure that does the UPDATE in the Inventory table and INSERT in the history table all with passed in @variables. – Edward Jan 02 '17 at 01:22
  • @bbruman did you find what you were looking for; or did you give up? Did you look into the LAG function I mentioned in my answer? Even if you don't select an answer from anyone you can still upvote useful comments and answers. Thanks – Edward Jan 16 '17 at 00:49
  • Hi @Edward, the LAG function is only for SQL server, it does not work in MySQL. I'm taking another approach in this (just calculating sold_qty in php to add to the table instead of trying to do it in mysql). – bbruman Jan 16 '17 at 15:05
  • Didn't know that but then I did find this [Simulate LAG in mySQL](http://stackoverflow.com/q/11303532/3527398) – Edward Jan 16 '17 at 15:48

1 Answers1

0

I think you are taking the wrong approach.

You seem to want an insert on the inventory table. When a new value is inserted or updated (or deleted), you then insert a row in the inventory_history table with the old and new values.

You then don't need an explicit insert on inventory_history.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Perhaps this is a solution, haven't thought it before. The thing is entries in the `inventory` table are updated hundreds of times a day, some for the same `sku`. My query is designed to just take a snapshot at midnight, every night, of newly updated inventory items, and put them in the `inventory_history` table – bbruman Jan 01 '17 at 23:09
  • @bbruman . . . Why do you really need the previous value if you can get it by querying the table? – Gordon Linoff Jan 01 '17 at 23:16
  • So that I can subtract the `new.Quantity` from this previous value and calculate the total amount sold ( so I will be doing 100 - 90, and 5100 - 5000 ) for the actual sold column value. I was just trying to minify the question because I need to be able to figure out how to match the `SKU` to get its previous quantity value – bbruman Jan 01 '17 at 23:21
  • @bbruman . . . That is probably better done when you are querying the data, particularly if you are only taking a snapshot at an arbitrary time each day. If you were archiving every transaction, then the `new`/`old` method would be appropriate. – Gordon Linoff Jan 02 '17 at 01:08