This is a part of a simplified relationship in a database.
I need a query that shows Machine name, ProductNo, ProductName, ProcessNo, Count of manufactured parts and the total time used for manufacturing, during a specified date interval.
The only way I could reach this result, was to write 2 queries:
qryResults:
Then built another query based on the first one.
qryResultsFinal:
The final query shows the expected results. But since I'm not that good in sql, I need to ask for some advice.
A sample database with a few record to test is attached.
Thanks you.
PS : At present, I create the sql of the second query dynamically in VBA and am not using the saved one.
I need a query that shows Machine name, ProductNo, ProductName, ProcessNo, Count of manufactured parts and the total time used for manufacturing, during a specified date interval.
The only way I could reach this result, was to write 2 queries:
qryResults:
SQL:
SELECT
C.MachineName,
P.ProductNo,
P.ProductName,
M.ProcessNo,
M.StartedFrom,
M.EndedOn,
M.CycleTime,
M.Detachment,
[CounterTo]-[CounterFrom] AS Quantity,
([Quantity]*([CycleTime]+[Detachment])) AS TotalProcessTime
FROM
tblProducts AS P INNER JOIN (tblMachines AS C INNER JOIN tblManu AS M ON C.MachinePK = M.MachineFK) ON P.ProductPK = M.ProductFK
ORDER BY
M.StartedFrom;
Then built another query based on the first one.
qryResultsFinal:
SQL:
SELECT
q.MachineName,
q.ProductNo,
q.ProductName,
q.ProcessNo,
Sum(q.Quantity) AS QuantityOfSum,
Round(Sum([TotalProcessTime]*24),2) AS TotalTime
FROM
qryResults AS q
WHERE
q.StartedFrom >#2024/5/2# AND q.EndedOn <#2024/5/7#
GROUP BY
q.MachineName, q.ProductNo, q.ProductName, q.ProcessNo
ORDER BY
Round(Sum([TotalProcessTime]*24),2) DESC;
The final query shows the expected results. But since I'm not that good in sql, I need to ask for some advice.
- Is there any better (simpler) way to reach this final result?
- As you see the date duration is hard coded into second query. I need to open the query in vba and work on the result with different date.
OpenRecordset("SELECT * FROM qryResults_FINAL WHERE StartedFrom >#2024/5/3# AND EndedOn <#2024/5/5#")
throws "Too few parameters" error.
If I remove the WHERE clause from the second query, still I'm facing the same error.
How can I open a recordset of the second query at different dates?
A sample database with a few record to test is attached.
Thanks you.
PS : At present, I create the sql of the second query dynamically in VBA and am not using the saved one.
Attachments
Last edited: