sum - Summing disctinct records MySQL -


i have table of inventory counts , need sum each item each location. count table log of counting

+-----------+---------+---------+-------------+---------------------+ | record_id | item_id | count   | location_id | time_stamp          | +-----------+---------+---------+-------------+---------------------+ |         1 |     129 |    3.00 |           1 | 2017-08-23 16:56:05 | |         2 |     129 |   14.00 |           2 | 2017-08-23 16:57:28 | |         3 |     129 |    4.00 |           1 | 2017-08-31 16:59:39 | |         4 |     129 |   14.00 |           2 | 2017-08-31 17:01:27 | |         5 |     133 |    4.00 |           1 | 2017-08-23 17:02:21 | |         6 |     133 |    0.00 |           2 | 2017-08-23 17:03:22 | |         7 |     133 |    8.00 |           3 | 2017-08-23 17:03:55 | |         8 |     133 |    4.00 |           1 | 2017-08-26 17:04:22 | |         9 |     133 |    1.00 |           2 | 2017-08-26 17:05:08 | |        10 |     133 |    9.00 |           3 | 2017-08-26 17:05:44 | +-----------+---------+---------+-------------+---------------------+ 

so item 129 has total of 18. first counted on 8/23, more recent count on 8/31 shows 4 in location 1 , 14 in location 2. need total count (sum) of each item in locations, based on recent count. appreciated.

not sure if want or not, should post expected result in op, can give want exactly.

you can try following sql:

select     t1.`item_id`,     sum(t1.`count`) sum_count yourtable t1 inner join (     select `item_id`, `location_id`, max(`time_stamp`) `time_stamp`     yourtable     group `item_id`, `location_id` ) t2 on t1.`item_id` = t2.`item_id` , t1.`location_id` = t2.`location_id` , t1.`time_stamp` = t2.`time_stamp` group t1.`item_id` 

and see demo in sqlfiddle.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -