473,466 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Query Problem

8 New Member
Hi All,

I have a query:

Expand|Select|Wrap|Line Numbers
  1. SELECT Sum(tbl_ExtrasOrder.OrderExtrasValue) AS SumOfOrderExtrasValue, Sum(tbl_FramesOrder.OrderFramesValue) AS SumOfOrderFramesValue, Sum(tbl_GlassOrder.OrderGlassValue) AS SumOfOrderGlassValue, Sum(tbl_ServicesOrder.OrderServicesValue) AS SumOfOrderServicesValue, Sum(tbl_WindowsOrder.OrderWindowsValue) AS SumOfOrderWindowsValue, tbl_ExtrasOrder.OrderExtrasDate, tbl_FramesOrder.OrderFramesDate, tbl_GlassOrder.OrderGlassDate, tbl_ServicesOrder.OrderServicesDate, tbl_WindowsOrder.OrderWindowsDate, tbl_Clients.ClientID, tbl_Clients.ClientSurname, tbl_Clients.ClientDesigner
  2. FROM ((((tbl_Clients LEFT JOIN tbl_ExtrasOrder ON tbl_Clients.ClientID = tbl_ExtrasOrder.ClientID) LEFT JOIN tbl_FramesOrder ON tbl_Clients.ClientID = tbl_FramesOrder.ClientID) LEFT JOIN tbl_GlassOrder ON tbl_Clients.ClientID = tbl_GlassOrder.ClientID) LEFT JOIN tbl_ServicesOrder ON tbl_Clients.ClientID = tbl_ServicesOrder.ClientID) LEFT JOIN tbl_WindowsOrder ON tbl_Clients.ClientID = tbl_WindowsOrder.ClientID
  3. GROUP BY tbl_ExtrasOrder.OrderExtrasDate, tbl_FramesOrder.OrderFramesDate, tbl_GlassOrder.OrderGlassDate, tbl_ServicesOrder.OrderServicesDate, tbl_WindowsOrder.OrderWindowsDate, tbl_Clients.ClientID, tbl_Clients.ClientSurname, tbl_Clients.ClientDesigner
  4. HAVING (((Sum(tbl_WindowsOrder.OrderWindowsValue)) Is Not Null) AND ((tbl_WindowsOrder.OrderWindowsDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_ServicesOrder.OrderServicesValue)) Is Not Null) AND ((tbl_ServicesOrder.OrderServicesDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_GlassOrder.OrderGlassValue)) Is Not Null) AND ((tbl_GlassOrder.OrderGlassDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_FramesOrder.OrderFramesValue)) Is Not Null) AND ((tbl_FramesOrder.OrderFramesDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_ExtrasOrder.OrderExtrasValue)) Is Not Null) AND ((tbl_ExtrasOrder.OrderExtrasDate) Between #" & dStartDate & "# And #" & dEndDate & "#))
  5. ORDER BY tbl_Clients.ClientSurname;
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
Apr 8 '08 #1
7 1307
FishVal
2,653 Recognized Expert Specialist
Hi, Shane.

Are you sure the query will return right results at all ???
Table join is basically table multiplying.
So, if, let us say, Client1 has 3 records in a first orders table and 4 records in the second one, then joining these three tables will give 12 records and each order in the first orders table will appear 4 times and from the second one 3 times. After that they will be summed.

Kind regards,
Fish
Apr 8 '08 #2
shane8960
8 New Member
1) Yes the query does return the right results, if I dont put the SUM for each field it brings up multiple records as you stated. However the SUM is only the SUM of the actual records, rather than the duplicates that where shown.

2) Ill try UNION instead of join see if that fixes it.

3) Theres 5 order tables as there are 5 different types of item they can order (Window, Glass, Frame, Extras & Services), each type of order is completely different (ie the tables have 10-20 fields - all different for each order type so would mean having 80 or so fields in a table with about a 80% or so redundancy).

Thanks, Shane
Apr 8 '08 #3
FishVal
2,653 Recognized Expert Specialist
Ok.

How do you set values of dStartDate and dEndDate?
Apr 8 '08 #4
shane8960
8 New Member
Ok.

How do you set values of dStartDate and dEndDate?
Basically I have a form, with controlsource set to the above (without the extra date criteria), I then two text boxes where the user enters a date, they then press a command button which takes the dates from the text boxes, assigns them to the dStartDate and dEndDate and sets the query above to the forms source and then refreshes the form.

Also - I cant see how Union would work in this case. I need to be able to distinguish which table each of the values came from as I need to break it down into "Windows Value", "Glass Value" etc for each client.
Apr 9 '08 #5
FishVal
2,653 Recognized Expert Specialist
Basically I have a form, with controlsource set to the above (without the extra date criteria), I then two text boxes where the user enters a date, they then press a command button which takes the dates from the text boxes, assigns them to the dStartDate and dEndDate and sets the query above to the forms source and then refreshes the form.
Are you sure you get it in proper m/d/y format?

Also - I cant see how Union would work in this case. I need to be able to distinguish which table each of the values came from as I need to break it down into "Windows Value", "Glass Value" etc for each client.
Not a problem. You may design UNION subqueries to return constant identifying source table.
Expand|Select|Wrap|Line Numbers
  1. SELECT 'Windows Value' AS txtOrderType, .... FROM tbl_WindowsOrder
  2. UNION
  3. SELECT 'Glass Value' AS txtOrderType, .... FROM tbl_GlassOrder;
  4.  
Regards,
Fish

P.S. I'm still confused as for what prevent JOIN to multiply your records. :)
P.P.S. Have you tried to move date criteria from HAVING clause to WHERE clause?
Apr 9 '08 #6
shane8960
8 New Member
Thanks Mate,

Used UNION and it works a treat.

Thanks again,

Shane
Apr 9 '08 #7
FishVal
2,653 Recognized Expert Specialist
You are welcome.

Best regards,
Fish
Apr 9 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
3
by: Brian Oster | last post by:
After applying security patch MS03-031 (Sql server ver 8.00.818) a query that used to execute in under 2 seconds, now takes over 8 Minutes to complete. Any ideas on what the heck might be going...
1
by: Jeff Blee | last post by:
I hope someone can help me get this graph outputing in proper order. After help from Tom, I got a graph to display output from the previous 12 months and include the average of that output all in...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
11
by: Andy_Khosravi | last post by:
My problem: I'm having trouble with a query taking much too long to run; a query without any criteria evaluating only 650 records takes over 300 seconds to run (over the network. On local drive...
4
by: Konrad Hammerer | last post by:
Hi! I have the following problem: I have a query (a) using another query (b) to get the amount of records of this other query (b), means: select count(MNR) as Number from...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.