Hi All,
I have a query:
[code=sql]SELECT Sum(tbl_ExtrasO rder.OrderExtra sValue) AS SumOfOrderExtra sValue, Sum(tbl_FramesO rder.OrderFrame sValue) AS SumOfOrderFrame sValue, Sum(tbl_GlassOr der.OrderGlassV alue) AS SumOfOrderGlass Value, Sum(tbl_Service sOrder.OrderSer vicesValue) AS SumOfOrderServi cesValue, Sum(tbl_Windows Order.OrderWind owsValue) AS SumOfOrderWindo wsValue, tbl_ExtrasOrder .OrderExtrasDat e, tbl_FramesOrder .OrderFramesDat e, tbl_GlassOrder. OrderGlassDate, tbl_ServicesOrd er.OrderService sDate, tbl_WindowsOrde r.OrderWindowsD ate, tbl_Clients.Cli entID, tbl_Clients.Cli entSurname, tbl_Clients.Cli entDesigner
FROM ((((tbl_Clients LEFT JOIN tbl_ExtrasOrder ON tbl_Clients.Cli entID = tbl_ExtrasOrder .ClientID) LEFT JOIN tbl_FramesOrder ON tbl_Clients.Cli entID = tbl_FramesOrder .ClientID) LEFT JOIN tbl_GlassOrder ON tbl_Clients.Cli entID = tbl_GlassOrder. ClientID) LEFT JOIN tbl_ServicesOrd er ON tbl_Clients.Cli entID = tbl_ServicesOrd er.ClientID) LEFT JOIN tbl_WindowsOrde r ON tbl_Clients.Cli entID = tbl_WindowsOrde r.ClientID
GROUP BY tbl_ExtrasOrder .OrderExtrasDat e, tbl_FramesOrder .OrderFramesDat e, tbl_GlassOrder. OrderGlassDate, tbl_ServicesOrd er.OrderService sDate, tbl_WindowsOrde r.OrderWindowsD ate, tbl_Clients.Cli entID, tbl_Clients.Cli entSurname, tbl_Clients.Cli entDesigner
HAVING (((Sum(tbl_Wind owsOrder.OrderW indowsValue)) Is Not Null) AND ((tbl_WindowsOr der.OrderWindow sDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Serv icesOrder.Order ServicesValue)) Is Not Null) AND ((tbl_ServicesO rder.OrderServi cesDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Glas sOrder.OrderGla ssValue)) Is Not Null) AND ((tbl_GlassOrde r.OrderGlassDat e) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Fram esOrder.OrderFr amesValue)) Is Not Null) AND ((tbl_FramesOrd er.OrderFramesD ate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Extr asOrder.OrderEx trasValue)) Is Not Null) AND ((tbl_ExtrasOrd er.OrderExtrasD ate) Between #" & dStartDate & "# And #" & dEndDate & "#))
ORDER BY tbl_Clients.Cli entSurname;[/code]
Basically its a query that links 5 order tables to a clients table using ClientID. Each order table DOES contain more than 1 record for each client. This query gets the sum on all the records for the client for each category. However there is a further twist, I need to be able to specify what dates I want the results to appear for (dStartDate & dEndDate, set when the user pressed the search button using VBA).
However the problem I am having is that say if the client has ordered some windows on the 01/01/2008 and some glass on the 01/02/2008, when I search for orders in january it brings both the orders in and the same for february, however I only want it to bring in the orders marked with the dates which fall into the category.
I know its a big query, if anybody has any advice on how I can fix it to do what I want or an alternative way of doing it, it would be much appreciated.
Thanks, Shane
I have a query:
[code=sql]SELECT Sum(tbl_ExtrasO rder.OrderExtra sValue) AS SumOfOrderExtra sValue, Sum(tbl_FramesO rder.OrderFrame sValue) AS SumOfOrderFrame sValue, Sum(tbl_GlassOr der.OrderGlassV alue) AS SumOfOrderGlass Value, Sum(tbl_Service sOrder.OrderSer vicesValue) AS SumOfOrderServi cesValue, Sum(tbl_Windows Order.OrderWind owsValue) AS SumOfOrderWindo wsValue, tbl_ExtrasOrder .OrderExtrasDat e, tbl_FramesOrder .OrderFramesDat e, tbl_GlassOrder. OrderGlassDate, tbl_ServicesOrd er.OrderService sDate, tbl_WindowsOrde r.OrderWindowsD ate, tbl_Clients.Cli entID, tbl_Clients.Cli entSurname, tbl_Clients.Cli entDesigner
FROM ((((tbl_Clients LEFT JOIN tbl_ExtrasOrder ON tbl_Clients.Cli entID = tbl_ExtrasOrder .ClientID) LEFT JOIN tbl_FramesOrder ON tbl_Clients.Cli entID = tbl_FramesOrder .ClientID) LEFT JOIN tbl_GlassOrder ON tbl_Clients.Cli entID = tbl_GlassOrder. ClientID) LEFT JOIN tbl_ServicesOrd er ON tbl_Clients.Cli entID = tbl_ServicesOrd er.ClientID) LEFT JOIN tbl_WindowsOrde r ON tbl_Clients.Cli entID = tbl_WindowsOrde r.ClientID
GROUP BY tbl_ExtrasOrder .OrderExtrasDat e, tbl_FramesOrder .OrderFramesDat e, tbl_GlassOrder. OrderGlassDate, tbl_ServicesOrd er.OrderService sDate, tbl_WindowsOrde r.OrderWindowsD ate, tbl_Clients.Cli entID, tbl_Clients.Cli entSurname, tbl_Clients.Cli entDesigner
HAVING (((Sum(tbl_Wind owsOrder.OrderW indowsValue)) Is Not Null) AND ((tbl_WindowsOr der.OrderWindow sDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Serv icesOrder.Order ServicesValue)) Is Not Null) AND ((tbl_ServicesO rder.OrderServi cesDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Glas sOrder.OrderGla ssValue)) Is Not Null) AND ((tbl_GlassOrde r.OrderGlassDat e) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Fram esOrder.OrderFr amesValue)) Is Not Null) AND ((tbl_FramesOrd er.OrderFramesD ate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Extr asOrder.OrderEx trasValue)) Is Not Null) AND ((tbl_ExtrasOrd er.OrderExtrasD ate) Between #" & dStartDate & "# And #" & dEndDate & "#))
ORDER BY tbl_Clients.Cli entSurname;[/code]
Basically its a query that links 5 order tables to a clients table using ClientID. Each order table DOES contain more than 1 record for each client. This query gets the sum on all the records for the client for each category. However there is a further twist, I need to be able to specify what dates I want the results to appear for (dStartDate & dEndDate, set when the user pressed the search button using VBA).
However the problem I am having is that say if the client has ordered some windows on the 01/01/2008 and some glass on the 01/02/2008, when I search for orders in january it brings both the orders in and the same for february, however I only want it to bring in the orders marked with the dates which fall into the category.
I know its a big query, if anybody has any advice on how I can fix it to do what I want or an alternative way of doing it, it would be much appreciated.
Thanks, Shane
Comment