473,545 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Additional Join in Query Results in Miscalculation

10 New Member
Hi, I am a chemical engineer trying to design a database and I am running into some trouble.

My database is going to be 'processing' raw data to get the figures we need to prepare the monthly reports. I am given data broken down by blends, plant that they are produced in, and amount, Adj Good Kgs. Each Blend belongs to a Product Group which in turn belongs to a Business Unit (the Business Units don't really come into my question though). My object is to get the sums of the data by Product Group, and then the cost by multiplying them by a price.

I have a query with the data already manipulated a little bit called All Calculated Values. That is where my Plant and Adj Good Kgs fields are from. The Blends and Prod Group table allows me to assign a Product Group and sum them that way.

My problem comes up with the totals query that I am trying to run. When I just have the query All Calculated Values and the table Blends and Prod Groups, it works fine (as long as there are no further relationships defined). It's when I bring in the Pricing table, which allows me to assign a price based on the plant and product group (both are necessary, and the Pricing table uses both of those fields) that I get HUGE values for my adj good kgs, like its adding each record up many times. I don't really understand it myself.

Is it appropriate to try to do calculated fields (ie a cost field that is Adj Good Kgs x Price) that aren't sums in a totals query? Should I just do that elsewhere? Why would it give me such big numbers when all I've done is add one more join?

Hopefully one of you guys can make sense of this. Let me post the code of the query when it's working (w/o the Pricing table and it's join) and when it's not (with Pricing).

(without)
Expand|Select|Wrap|Line Numbers
  1. SELECT [All Calculated Values Jan].Plant, [Blends and Prod Groups].[Product Group], Sum([All Calculated Values Jan].[Adj Good Kgs]) AS [SumOfAdj Good Kgs]
  2. FROM [Blends and Prod Groups] INNER JOIN [All Calculated Values Jan] ON [Blends and Prod Groups].Blend = [All Calculated Values Jan].Blend
  3. GROUP BY [All Calculated Values Jan].Plant, [Blends and Prod Groups].[Product Group];
  4.  
(with - additional table and join are bolded)
Expand|Select|Wrap|Line Numbers
  1. SELECT [All Calculated Values Jan].Plant, [Blends and Prod Groups].[Product Group], Sum([All Calculated Values Jan].[Adj Good Kgs]) AS [SumOfAdj Good Kgs]
  2. FROM ([Blends and Prod Groups] INNER JOIN [All Calculated Values Jan] ON [Blends and Prod Groups].Blend = [All Calculated Values Jan].Blend) INNER JOIN Pricing ON [Blends and Prod Groups].[Product Group] = Pricing.[Product Group]
  3. GROUP BY [All Calculated Values Jan].Plant, [Blends and Prod Groups].[Product Group];
  4.  
Thanks in advance, for anyone who attempts this one!
Jun 29 '07 #1
4 1772
nico5038
3,080 Recognized Expert Specialist
Without going into detail, I'll explain what the cuase is of the big numbers.
When JOINing two tables a so-called "cartesian product" is created. This is a "large" table containing for each row of one table all rows of the other table.
You can try this by creating a table with one field Year and enter 2006 and 2007 as rows and another table with one field Month and the values 1 till 12. When you place both tables in a select like:
select * from tblYear, tblMonth
You'll get 24rows.

In your case the JOIN is limited by a comparison in the ON clause, but when you use non-unique fields, the Cartesian product will cause you trouble.

In general I start with creating a query to calculate the detail data (like the amount) and add additional descriptions and that query I use in a GoupBy query to get the aggregation values.

Nic;o)
Jun 30 '07 #2
tweeterbot
10 New Member
Without going into detail, I'll explain what the cuase is of the big numbers.
When JOINing two tables a so-called "cartesian product" is created. This is a "large" table containing for each row of one table all rows of the other table.
You can try this by creating a table with one field Year and enter 2006 and 2007 as rows and another table with one field Month and the values 1 till 12. When you place both tables in a select like:
select * from tblYear, tblMonth
You'll get 24rows.

In your case the JOIN is limited by a comparison in the ON clause, but when you use non-unique fields, the Cartesian product will cause you trouble.

In general I start with creating a query to calculate the detail data (like the amount) and add additional descriptions and that query I use in a GoupBy query to get the aggregation values.

Nic;o)
That makes a lot more sense now.

So are you recommending that I just go ahead and break it up into different queries: one totals query for aggregating and one regular query to calculate price?
Jul 2 '07 #3
nico5038
3,080 Recognized Expert Specialist
Affirmative, first the "detail" query with the basic calculations (like Price*Quantity) and than the GroupBy to get on the needed field(s) the Sum(), Average(), etc. of the calculated value.

Success !

Nic;o)
Jul 3 '07 #4
tweeterbot
10 New Member
Thank you so much! This has been a great help for me in understanding!
Jul 5 '07 #5

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

Similar topics

0
2441
by: Doug Reese | last post by:
hello, i have what seems to me a very common operation i'm performing. i need to find the balance on an invoice. i was not having any problems until the production server was upgraded to mysql v4.0.13-standard for pc-linux. there must be a better way to query for this information than the method i'm using, since the result with v4.0 is...
0
2350
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not null default 0 auto_increment, a1 varchar(64) not null default '',
13
1890
by: kieran | last post by:
Hi, I have the following SQL statement which is pulling a few details from a database. As you can see, there is only the one table from which i am creating a temporary copy. The reason I do this is because in the table i only have the 'standIn' listed by integer and i want to return the 'standIn' by name. I hope this is clear enough.
2
2650
by: terence.parker | last post by:
I am often faced with the dilemma of whether to use a JOIN query across three tables in order to grab a bunch of results - or whether to create another table to represent what I want. The latter is less normalised, but seems less computationally expensive to me(?). Basicially what I have is: Friend (uid1,uid2,fid) ->...
2
3337
by: HS Hartkamp | last post by:
Hi all, I have a table with analysis-results for various months. An item can -for a particular month- have the result 'list1', 'list2' or 'list3' depending on the result of the analysis. Next, I want to get an insight in how the various items change over time. For that, I compare the results of one month with the results of another...
3
4132
by: Martini | last post by:
Hello all. I have quite disturbing situation where I am not happy about the way how SQL handles the query. Situation is related to using user function in INNER JOIN select. Although the problem occured in more complex situation the query can be simplified to following example with same results: There is an user function, that can be as...
3
16498
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so all data from the separate tables is shown in a view (instead of the reference id's pointing to the separate tables...) I have some troubles...
12
13163
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
14
2481
by: cjakeman | last post by:
Hi, Solved a little mystery yesterday when I built a form that combined 2 tables with a 1:M relationship and relational integrity. All the correct data was visible on the form but, if I tried to edit any of the fields, the PC bleeped. Seems it was due to the query the form was based on. Again, editing any of the fields of the query...
0
7496
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7428
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7941
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5354
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.