473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problems with joins

I'm having trouble with joins...

TableA includes the fields name, order_number and value1.
Tableb includes the fields order number and value2

I'm joining on Order Number.
I want to return Name, TableA.order_number, TableB.order_number, value1
and value2.

I want to return all order_numbers from TableA and any from TableB that
are in TableA.

My script currently has TableA LEFT OUTER JOIN TableB On
TableA.order_number = TableB.order_number

I know it is incorrect because the sum of TableA.Value1 is 100, but
after my left outer join with TableB the sum of Value1 is 50.

Any ideas?

Regards,
Ciarán

Jul 23 '05 #1
3 1163
On 12 Apr 2005 09:38:44 -0700, ch********@hotmail.com wrote:
I'm having trouble with joins...

TableA includes the fields name, order_number and value1.
Tableb includes the fields order number and value2

I'm joining on Order Number.
I want to return Name, TableA.order_number, TableB.order_number, value1
and value2.

I want to return all order_numbers from TableA and any from TableB that
are in TableA.

My script currently has TableA LEFT OUTER JOIN TableB On
TableA.order_number = TableB.order_number

I know it is incorrect because the sum of TableA.Value1 is 100, but
after my left outer join with TableB the sum of Value1 is 50.

Any ideas?

Regards,
Ciarán


Hi Ciarán,

Can you post the complete query, please? Even better would be if you
include the DDL (CREATE TABLE statements) for the tables as well,
preferably including some sample data (posted as INSERT statements).

That will allow us to review your code, run some tests and come up with
a suggestion.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Here is the script

The sum of Shpt_Leg_List_CLC changes between Script1 and Script2 and
the only change I think I have made is the join to TableB

Script1
SELECT Customer_Number AS Customer_Number, ID_Code AS ID_Code,
LEFT(Ship_Day, 4) AS Ship_Month, SUM(Shpt_Leg_List_CLC) AS
Shpt_Leg_List_CLC,
SUM(Shpt_Leg_Net_CLC) AS Shpt_Leg_Net_CLC,
SUM(Shpt_Leg_List_LC) AS Shpt_Leg_List_LC, SUM(Shpt_Leg_Net_LC) AS
Shpt_Leg_Net_LC,
SUM(Shpt_Leg_Ref_List) AS Shpt_Leg_Ref_List,
SUM(Shpt_Leg_Ref_Net) AS Shpt_Leg_Ref_Net, SUM(Shpt_Leg_Prod_Units) AS
Shpt_Leg_Prod_Units,
SUM(Shpt_Leg_Optn_Units) AS Shpt_Leg_Optn_Units,
SUM(Shpt_Leg_Supt_Units) AS Shpt_Leg_Supt_Units,
Shpt_Leg_MCC_Units AS Shpt_Leg_MCC_Units,
Department, Misc_Charge_Code AS TableA_DataV1, Order_Section
INTO TableA_DataV2
FROM TableA
GROUP BY Customer_Number, ID_Code, LEFT(Ship_Day, 4),
Shpt_Leg_MCC_Units, Department, Misc_Charge_Code, Order_Section
ORDER BY LEFT(Ship_Day, 4), Customer_Number, ID_Code, Department,
Shpt_Leg_MCC_Units, Misc_Charge_Code

Script2
SELECT TableA.Customer_Number AS Customer_Number, TableA.ID_Code AS
ID_Code,
LEFT(TableA.Ship_Day, 4) AS Ship_Month,
SUM(TableA.Shpt_Leg_List_CLC) AS Shpt_Leg_List_CLC,
SUM(TableA.Shpt_Leg_Net_CLC) AS Shpt_Leg_Net_CLC,
SUM(TableA.Shpt_Leg_List_LC)
AS Shpt_Leg_List_LC, SUM(TableA.Shpt_Leg_Net_LC)
AS Shpt_Leg_Net_LC,
SUM(TableA.Shpt_Leg_Ref_List) AS
Shpt_Leg_Ref_List, SUM(TableA.Shpt_Leg_Ref_Net)
AS Shpt_Leg_Ref_Net,
SUM(TableA.Shpt_Leg_Prod_Units) AS Shpt_Leg_Prod_Units,
SUM(TableA.Shpt_Leg_Optn_Units) AS
Shpt_Leg_Optn_Units, SUM(TableA.Shpt_Leg_Supt_Units)
AS Shpt_Leg_Supt_Units, TableA.Shpt_Leg_MCC_Units
AS Shpt_Leg_MCC_Units, TableA.Department,
TableA.Misc_Charge_Code AS TableA_DataV1,
TableA.Order_Section,
TableB.Sales_Order_Number
INTO TableA_DataV2
FROM TableA LEFT OUTER JOIN
TableB ON TableA.Order_Section =
TableB.Sales_Order_Number
GROUP BY TableA.Customer_Number, TableA.ID_Code, LEFT(TableA.Ship_Day,
4),
TableA.Shpt_Leg_MCC_Units, TableA.Department,
TableA.Misc_Charge_Code, TableA.Order_Section,
TableB.Sales_Order_Number
ORDER BY LEFT(TableA.Ship_Day, 4), TableA.Customer_Number,
TableA.ID_Code,
TableA.Department, TableA.Shpt_Leg_MCC_Units,
TableA.Misc_Charge_Code

Jul 23 '05 #3
On 13 Apr 2005 01:37:09 -0700, ch********@hotmail.com wrote:
Here is the script

The sum of Shpt_Leg_List_CLC changes between Script1 and Script2 and
the only change I think I have made is the join to TableB

(snip)

Hi Ciarán,

Does only the sum of Shpt_Leg_List_CLC change, or do all the other sums
change as well?

Did you factor in that the total from one group in query 1 may be
divided over several groups in query 2, due to the extra column in the
GROUP BY clause (TableB.Sales_Order_Number)?

Are you sure that the total from query 1 is HIGHER than the total from
query 2? I could explain a LOWER total, not a HIGHER total!

I see no obvious problems with the code you posted. If you want me to
investigate this further, you'll really have to poste CREATE TABLE and
INSERT statements as well, so that I can reproduce the problem on my
computer.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Randell D. | last post by:
Folks, I've not done a join before... and I need some help - I believe my problem is because I am attempting to join two tables which both have a hash column in common, however their names in...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
4
by: michaelnewport | last post by:
Greetings, I like to write my inner joins as below, but someone at work tells me its not as 'performant' as using the 'inner join' statement. Is this true ? Is there a better way to write it...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
5
by: Scott | last post by:
I have a customer that had developed an Access97 application to track their business information. The application grew significantly and they used the Upsizing Wizard to move the tables to SQL...
0
by: news.microsoft.com | last post by:
first i apologize for the potential crosspost. this group gets far more traffic. now that i have that out of the way... ;) apparently using AutoGenerated Commands on Command objects does not work...
2
by: narendra vuradi | last post by:
Hi I have a requirement where in i haev to convert the SQL from Oracle to the one which will run on the SQL server. in the Oracle Query i am doing multiple joins, between some 13 tables. and...
5
by: Bogdan | last post by:
Hi, I have a stored procedure that uses JOINs to return columns from multiple tables. I also have another stored proc that that takes a series of params and updates multiple tables. I used the...
36
by: TC | last post by:
I've used Access for many years. Several times, I've encountered a bug which I refer to as the "Vanishing Joins" bug. When it happens, joins vanish randomly from queries. More specifically, all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.