My Table is
itemcode in out sum value datetime PP
-------|---|---|-----------|-----------|---------
A1234 | 1 | 0 | 100 |04/03/2009 | E01
A1234 | 0 | 1 | -100 |05/03/2009 | E01
A1234 | 1 | 0 | 100 |06/03/2009 | E01
A1234 | 0 | 1 | -100 |07/03/2009 | E01
A1234 | 1 | 0 | 100 |08/03/2009 | E01
A1234 | 9 | 0 | 900 |09/03/2009 | S01
A1234 | 0 | 3 | -300 |10/03/2009 | S01
A1234 | 8 | 0 | 800 |11/03/2009 | S01
A1235 | 9 | 0 | 900 |12/03/2009 | E01
A1235 | 0 | 2 | -200 |13/03/2009 | E01
A1235 | 0 | 3 | -300 |14/03/2009 | E01
A1235 | 8 | 0 | 800 |15/03/2009 | S01
Result would be:
itemcode Remain sum value datetime PP
-------|-------|-----------|-----------|----
A1234 | 1 | 100 |07/03/2009 | E01
A1234 | 6 | 600 |09/03/2009 | S01
A1234 | 8 | 800 |11/03/2009 | S01
A1235 | 4 | 400 |12/03/2009 | E01
A1235 | 8 | 800 |15/03/2009 | S01
How to calculate in different rows and find the last purchase late. Group by itemcode and PP
Thanks
2条答案
按热度按时间of1yzvn41#
Hugo,
See the code below. You did not give a table name, so I named it as @a. I avoided column itemcode as it is same for all the records.
Result:
enxuqcxy2#
This question was asked a long time ago. The desired output is wrong according to the input data and the grouping that was requested. It is a group by answer after joining in_count and out_count into a single field/column/result.
Schema (MySQL v5.7)
Query
View on DB Fiddle