Morning all,
I've read several threads on this forum about correlated subqueries and the problems that others have had whilst creating them, but none of what I've read has seemed to help. In this query, piece number is basically an ID for a piece of fabric. Each piece of fabric can be using in any number of stock transactions. Currently I'm running two queries which between them return a list of transactions for each piece number, along with a field "Warping_Diff", which is the difference in days between the current date and a date specified by the user, at the beginning of the first query.
The records returned from these two queries contain all transactions for all piece numbers within a date range also specified by the user. This is what I want to happen, with one small change: I want only one record for each piece number, in particular, I want the record with the lowest value in the "Warping_Diff" field for each piece number. I've written the correlated subquery below, based on a very similar scenario that I saw in an SQL book, but it doesn't return any results. When I wrote this query in SQL view in query builder and then ran it, query builder changed zqryWarping_Data2 to "a", so I attempted to execute the query in VBA by storing it as a string, but this generated an error message saying that an SQL statement had been expected.
Here's the query:
Any help would be greatly appreciated.
Thanks,
Matt
I've read several threads on this forum about correlated subqueries and the problems that others have had whilst creating them, but none of what I've read has seemed to help. In this query, piece number is basically an ID for a piece of fabric. Each piece of fabric can be using in any number of stock transactions. Currently I'm running two queries which between them return a list of transactions for each piece number, along with a field "Warping_Diff", which is the difference in days between the current date and a date specified by the user, at the beginning of the first query.
The records returned from these two queries contain all transactions for all piece numbers within a date range also specified by the user. This is what I want to happen, with one small change: I want only one record for each piece number, in particular, I want the record with the lowest value in the "Warping_Diff" field for each piece number. I've written the correlated subquery below, based on a very similar scenario that I saw in an SQL book, but it doesn't return any results. When I wrote this query in SQL view in query builder and then ran it, query builder changed zqryWarping_Data2 to "a", so I attempted to execute the query in VBA by storing it as a string, but this generated an error message saying that an SQL statement had been expected.
Here's the query:
Code:
SELECT PieceNo, Warp_Stock_Transaction_ID, Warping_Diff
FROM zqryWarping_Data2 AS a
WHERE (SELECT min(Warping_Diff) FROM zqryWarping_Data2 as b WHERE a.PieceNo = b.PieceNo);
Any help would be greatly appreciated.
Thanks,
Matt