I have 3 separate tables to track inventory: Count for actual inventory counts, Build which uses/decreases inventory, and PurchaseOrder which adds to inventory. I've built a union query to bring all those records together for use in a DataPilot in Calc.
SELECT "Part", "Date" AS "EntryDate", CONCAT( CONCAT( TO_CHAR( "Date", 'YYYY/MM/DD' ), ' ' ), "Label" ) AS "DateLabel", "Qty" FROM
(
SELECT 'Count' AS "Label", "PartCountDateTime" AS "Date", "PartID" AS "Part", "PartCountQty" AS "Qty" FROM "PartCount" AS "A"
UNION
SELECT 'Build' AS "Label", "BuildDate" AS "Date", "PartID" AS "Part", - "BuildQtyAdjusted" AS "Qty" FROM "Build" AS "A"
UNION
SELECT 'Purchase Order' AS "Label", "PODate" AS "Date", "PartID" AS "Part", "POQty" AS "Qty" FROM "PurchaseOrder" AS "A"
)
ORDER BY "Part", "DateLabel"
And I can get a useful/current inventory quantity by adding a date condition:
SELECT "Part", CONCAT( CONCAT( TO_CHAR( "Date", 'YYYY/MM/DD' ), ' ' ), "Label" ) AS "LabelDate", "Qty" FROM
(
SELECT 'Count' AS "Label", "PartCountDateTime" AS "Date", "PartID" AS "Part", "PartCountQty" AS "Qty" FROM "PartCount" AS "A" WHERE "A"."Date" = ( SELECT MAX( "B"."PartCountDateTime" ) FROM "PartCount" AS "B" WHERE "B"."PartID" = "A"."PartID" )
UNION
SELECT 'Build' AS "Label", "BuildDate" AS "Date", "PartID" AS "Part", - "BuildQtyAdjusted" AS "Qty" FROM "Build" AS "A" WHERE "BuildDate" >= ( SELECT MAX( "PartCountDateTime" ) FROM "PartCount" AS "B" WHERE "B"."PartID" = "A"."PartID" )
UNION
SELECT 'Purchase Order' AS "Label", "PODate" AS "Date", "PartID" AS "Part", "POQty" AS "Qty" FROM "PurchaseOrder" AS "A" WHERE "PODate" >= ( SELECT MAX( "PartCountDateTime" ) FROM "PartCount" AS "B" WHERE "B"."PartID" = "A"."PartID" )
)
ORDER BY "Part", "LabelDate"
I give all this background to hopefully avoid fully defining each table and giving sample data for each. However, if that's needed to get a solution.... Anyway, on with the question.
Sample result generated by first query:
Part EntryDate DateLabel Qty
----- ------------ ------------ ----
0 02/17/09 12:00 AM 2009/02/17 Count 1234
0 02/23/09 12:00 AM 2009/02/23 Build -236
0 02/25/09 12:00 AM 2009/02/25 Purchase Order 234
1 02/17/09 12:00 AM 2009/02/17 Count 3432
1 02/20/09 12:00 AM 2009/02/20 Count 3500
1 02/25/09 12:00 AM 2009/02/25 Build -345
1 02/25/09 12:00 AM 2009/02/25 Purchase Order 343
1 02/26/09 12:00 AM 2009/02/26 Count 3400
1 02/26/09 12:00 AM 2009/02/26 Purchase Order 34
What I need added, is a (sub)total after each Purchase Order, of the quantities, from the most recent Count to the Purchase Order.
Giving a result set of:
Part EntryDate DateLabel Qty
----- ------------ ------------ ----
0 02/17/09 12:00 AM 2009/02/17 Count 1234
0 02/23/09 12:00 AM 2009/02/23 Build -236
0 02/25/09 12:00 AM 2009/02/25 Purchase Order 234
0 02/25/09 12:00 AM 2009/02/25 OverShortTtl 1232
1 02/17/09 12:00 AM 2009/02/17 Count 3432
1 02/20/09 12:00 AM 2009/02/20 Count 3500
1 02/25/09 12:00 AM 2009/02/25 Build -345
1 02/25/09 12:00 AM 2009/02/25 Purchase Order 343
1 02/25/09 12:00 AM 2009/02/25 OverShortTtl 3498
1 02/26/09 12:00 AM 2009/02/26 Count 3400
1 02/26/09 12:00 AM 2009/02/26 Purchase Order 34
1 02/26/09 12:00 AM 2009/02/26 OverShortTtl 3434
If possible, I'd like to do it just with queries. Multiple queries are fine, I'd just like to avoid scripts and code if possible. If that's no possible, then so be it.