I have one table(could only have one) that looks like seen below.
It contains a plant code, an article number and batches related to that article.
plant - code - batch - value - volume - added_date
A1 1000 A1 10 20 date
A1 2000 A7 20 15 date
A1 1000 A1 5 10 later than first A1 batch
A2 200 A8 10 9 date
A1 2000 A10 20 20 date
How could I present this information like this: for each plant and code, show the DISTINCT
number of batches and the volume for only the latest added batch(note that code 1000 in plant A1
is showing only 10 as that is the volume of the latest added batch).
plant - code - batches - volume
A1 1000 1 10
A1 2000 2 35
A2 200 1 9
I got some help from a guy here earlier, and that query works great until I tried to get the volume from only the latest batches if there is two batches with same number for the same product on the same plant.
SELECT code, plant,
COUNT(DISTINCT(batch)) as Batches,
SUM(value) as TotalValue,
SUM(volume) as TotalVolume,
SUM(value * risk) as TotalRisk,
FROM lists
GROUP BY code, plant
I guess you could do it with some concat and self joins but I am not skilled enough to work with only one table :/
Thanks in advance!
Edit: Well, actually the answer that Adrian linked to solved the described issue...however it seems to be to hard to do make everything else work with that solution. I did only describe a part of my "problem", what I thought was enough to then solve the rest myself. But I was wrong. Not only do I need the latest added batch and volume, I also need to do some SUM operations on the batches. I am thinking that it may be best for me to place the data into two different tables and work from there.