Hello all,
I'm trying to build a query that sounds simple in concept, but I'm having problems getting my head around it and would like some advice. I have a table that stores stock transactions and so has records that consist of a transaction date, the product's ID, the quantity and transaction type. There are two particular transaction types that I am trying to work with. These are when a product is moved to and from our factory floor. This is simple enough as these two transaction types each have a unique ID, and so I have easily produced a query that shows a list of every one of these transactions within a specified date range and is sorted by the product name and then the date. So it looks something like:
Product 1 10/05/2009 Issued to Factory Floor
Product 1 11/05/2009 Returned from Factory Floor
Product 2 11/05/2009 Issued to Factory Floor
etc.
There is one last attribute that I would like to display with these values, and it is the difficult part, the quantity in stock at the time. This can be calculated from the other transactions in the table. Each transaction record has a received quantity field and an adjusted quantity field, only one of which will be in use in any given record (not the best design ever, I know.) So to calculate the stock quantity for any given date, I simply run a query over all of the data where the transaction date is <= the given date, group by product number and sum the received quantity and the adjusted quantity. I can then place another query over the top to add the two summed values together and I have a snapshot of how much stock we had of any given product on my specified date.
However, I am having problems linking this kind of structure to the above query. It would mean that the two queries (one to group and sum, the other to add the results,) would need to be run for each row in the query results, to give the stock quantity of the given product, on that transaction date. I have tried writing a subquery to go into a new field on the first query, but I'm not sure how I would do so, I want to select records from the table where the transaction date and product ID match that of the current record, group and sum them, and then add the results of the sum together. I'm really struggling with where to start on the syntax of this, any help would be greatly appreciated.
Thanks,
Matthew
P.S. This is the SQL of the grouping and summing query, if I could link this in as a subquery I think I would be onto the right track:
But even to link these two fields (received and adjusted quantities) separately to my query, what sytax would I use? I actually want to use this query as a data source, not just for criteria. I have used subqueries before where I use another select query in the where clause, but I want the above in the from clause really.
I'm trying to build a query that sounds simple in concept, but I'm having problems getting my head around it and would like some advice. I have a table that stores stock transactions and so has records that consist of a transaction date, the product's ID, the quantity and transaction type. There are two particular transaction types that I am trying to work with. These are when a product is moved to and from our factory floor. This is simple enough as these two transaction types each have a unique ID, and so I have easily produced a query that shows a list of every one of these transactions within a specified date range and is sorted by the product name and then the date. So it looks something like:
Product 1 10/05/2009 Issued to Factory Floor
Product 1 11/05/2009 Returned from Factory Floor
Product 2 11/05/2009 Issued to Factory Floor
etc.
There is one last attribute that I would like to display with these values, and it is the difficult part, the quantity in stock at the time. This can be calculated from the other transactions in the table. Each transaction record has a received quantity field and an adjusted quantity field, only one of which will be in use in any given record (not the best design ever, I know.) So to calculate the stock quantity for any given date, I simply run a query over all of the data where the transaction date is <= the given date, group by product number and sum the received quantity and the adjusted quantity. I can then place another query over the top to add the two summed values together and I have a snapshot of how much stock we had of any given product on my specified date.
However, I am having problems linking this kind of structure to the above query. It would mean that the two queries (one to group and sum, the other to add the results,) would need to be run for each row in the query results, to give the stock quantity of the given product, on that transaction date. I have tried writing a subquery to go into a new field on the first query, but I'm not sure how I would do so, I want to select records from the table where the transaction date and product ID match that of the current record, group and sum them, and then add the results of the sum together. I'm really struggling with where to start on the syntax of this, any help would be greatly appreciated.
Thanks,
Matthew
P.S. This is the SQL of the grouping and summing query, if I could link this in as a subquery I think I would be onto the right track:
Code:
SELECT tabStock_Transaction.Ticket_ID, Sum(tabStock_Transaction.Received_Qty_Mtrs) AS SumOfReceived_Qty_Mtrs, Sum(tabStock_Transaction.Adjusted_Qty_Mtrs) AS SumOfAdjusted_Qty_Mtrs
FROM tabStock_Transaction
WHERE (((tabStock_Transaction.Transaction_Date)<=#9/29/2008#) AND ((tabStock_Transaction.Trans_Error)<>-1) AND ((tabStock_Transaction.Cancel)<>-1))
GROUP BY tabStock_Transaction.Ticket_ID;