473,513 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL - joining 5 tables - not all records retrieved

Hi all!

The SQL is below. Here is the scenario. The table Production contains all
of the production information for our plant. Primary key is Job #, ProDate,
Shift, Machine #. The table T_Scrap contains a breakdown of the scrap
information. It has the same primary keys as the Production table. But the
Secondary scrap is recorded against the day it is discovered, rather then
the day it was cast, so for the problem I'm trying to solve, Shift and
Machine are irrelavent.

I'm trying to get all of the production information summarized by week. If
there is Secondary Scrap, I'd like to add that data, and if there's External
Scrap, I'd like to add that data, and if there's Customer Returns, I'd like
to add that data. But even if there is no scrap or returns, I should still
get all Production information summarized by week.

The query I've developed below is only returning 44 rows, and I know there
should be at least 500. So it appears as though rows are being dropped.
But logically, the LEFT Joins seem correct. Additionally, the totals I'm
getting are incorrect (in some cases by a factor of exactly 18).

Any light that can be shed on this would be greatly appreciated.

cheers,
Matt.

SELECT Production.[Job #],
Year([Production].[ProDate]) & "-" & WeekNumber([Production].[ProDate]) AS
YearWeek,
Machine.Unit,
Sum(T_Scrap.[Scrap Quantity]) AS SecondaryScrap,
Sum(T_Scrap_1.[Scrap Quantity]) AS ExternalScrap,
Sum([Production].[Cast Shots]*[Job Information Link].[Cavities]) AS
CastParts
FROM (((Production LEFT JOIN T_Scrap ON (Production.ProDate =
T_Scrap.ProDate) AND (Production.[Job #] = T_Scrap.[Job #])) LEFT JOIN
T_Scrap AS T_Scrap_1 ON (Production.ProDate = T_Scrap_1.ProDate) AND
(Production.[Job #] = T_Scrap_1.[Job #])) INNER JOIN [Job Information Link]
ON Production.[Job #] = [Job Information Link].[Job #]) INNER JOIN Machine
ON Production.[Machine #] = Machine.[Machine #]
WHERE (((T_Scrap.ScrapCode) Between "200" And "204")
AND ((T_Scrap_1.ScrapCode)="205")
AND ((Production.[Cast Shots])>0))
GROUP BY Production.[Job #], Year([Production].[ProDate]) & "-" &
WeekNumber([Production].[ProDate]), Machine.Unit;
Nov 12 '05 #1
3 1697
User OUTER joins instead of inner joins. then you'll see all your
records. In the QBE grid, right-click on the join and then choose
option 2 or option 3.
Nov 12 '05 #2
"Matt." wrote:
Hi all!

The SQL is below. Here is the scenario. The table Production contains all
of the production information for our plant. Primary key is Job #, ProDate,
Shift, Machine #. The table T_Scrap contains a breakdown of the scrap
information. It has the same primary keys as the Production table. But the
Secondary scrap is recorded against the day it is discovered, rather then
the day it was cast, so for the problem I'm trying to solve, Shift and
Machine are irrelavent.

I'm trying to get all of the production information summarized by week. If
there is Secondary Scrap, I'd like to add that data, and if there's External
Scrap, I'd like to add that data, and if there's Customer Returns, I'd like
to add that data. But even if there is no scrap or returns, I should still
get all Production information summarized by week.

The query I've developed below is only returning 44 rows, and I know there
should be at least 500. So it appears as though rows are being dropped.
But logically, the LEFT Joins seem correct. Additionally, the totals I'm
getting are incorrect (in some cases by a factor of exactly 18).

Any light that can be shed on this would be greatly appreciated.

cheers,
Matt.

SELECT Production.[Job #],
Year([Production].[ProDate]) & "-" & WeekNumber([Production].[ProDate]) AS
YearWeek,
Machine.Unit,
Sum(T_Scrap.[Scrap Quantity]) AS SecondaryScrap,
Sum(T_Scrap_1.[Scrap Quantity]) AS ExternalScrap,
Sum([Production].[Cast Shots]*[Job Information Link].[Cavities]) AS
CastParts
FROM (((Production LEFT JOIN T_Scrap ON (Production.ProDate =
T_Scrap.ProDate) AND (Production.[Job #] = T_Scrap.[Job #])) LEFT JOIN
T_Scrap AS T_Scrap_1 ON (Production.ProDate = T_Scrap_1.ProDate) AND
(Production.[Job #] = T_Scrap_1.[Job #])) INNER JOIN [Job Information Link]
ON Production.[Job #] = [Job Information Link].[Job #]) INNER JOIN Machine
ON Production.[Machine #] = Machine.[Machine #]
WHERE (((T_Scrap.ScrapCode) Between "200" And "204")
AND ((T_Scrap_1.ScrapCode)="205")
AND ((Production.[Cast Shots])>0))
GROUP BY Production.[Job #], Year([Production].[ProDate]) & "-" &
WeekNumber([Production].[ProDate]), Machine.Unit;


Hmmmm, I like autonumbers. I think that'd simplify things in your life.
Probably too late not tho.

You need to do some debugging. Copy the query to a new query and open the new
one in design. Remove the Totals thus making it a Select query. Now run it and
review the results. You might as well see what the detail is and what you are
grouping.

Now start futzing with your Where clause. That's your culprit. For
example....I think you either have too many ANDs or not enough ORs. I'd wager
the lone
WHERE (((T_Scrap.ScrapCode) Between "200" And "204")
AND ((T_Scrap_1.ScrapCode)="205")
is it.and mist likely the
AND ((T_Scrap_1.ScrapCode)="205")
line.
Maybe it needs to be
WHERE (((T_Scrap.ScrapCode) Between "200" And "204")
AND (T_Scrap_1.ScrapCode="205" Or T_Scrap_1.ScrapCod Is Null)
since T_Scrap_1 may not have a record.

The bottom line is your filter is doing too much filtering. You need to find
out where. One you figure that out then convert back to a totals.
Nov 12 '05 #3
Hi again!

I ended up breaking the query down into 3 separate queries (One each for
primary scrap, secondary scrap, and customer returns) each keyed by YearWeek
and JobNo, and then using another query to join them together.

It runs a little slower than I wanted, but at least I know the data's right.

cheers,
Matt.

"Matt." <ma*******************@hotmail.com> wrote in message
news:0M********************@news20.bellglobal.com. ..
Hi all!

The SQL is below. Here is the scenario. The table Production contains all of the production information for our plant. Primary key is Job #, ProDate, Shift, Machine #. The table T_Scrap contains a breakdown of the scrap
information. It has the same primary keys as the Production table. But the Secondary scrap is recorded against the day it is discovered, rather then
the day it was cast, so for the problem I'm trying to solve, Shift and
Machine are irrelavent.

I'm trying to get all of the production information summarized by week. If there is Secondary Scrap, I'd like to add that data, and if there's External Scrap, I'd like to add that data, and if there's Customer Returns, I'd like to add that data. But even if there is no scrap or returns, I should still get all Production information summarized by week.

The query I've developed below is only returning 44 rows, and I know there
should be at least 500. So it appears as though rows are being dropped.
But logically, the LEFT Joins seem correct. Additionally, the totals I'm
getting are incorrect (in some cases by a factor of exactly 18).

Any light that can be shed on this would be greatly appreciated.

cheers,
Matt.

SELECT Production.[Job #],
Year([Production].[ProDate]) & "-" & WeekNumber([Production].[ProDate]) AS YearWeek,
Machine.Unit,
Sum(T_Scrap.[Scrap Quantity]) AS SecondaryScrap,
Sum(T_Scrap_1.[Scrap Quantity]) AS ExternalScrap,
Sum([Production].[Cast Shots]*[Job Information Link].[Cavities]) AS
CastParts
FROM (((Production LEFT JOIN T_Scrap ON (Production.ProDate =
T_Scrap.ProDate) AND (Production.[Job #] = T_Scrap.[Job #])) LEFT JOIN
T_Scrap AS T_Scrap_1 ON (Production.ProDate = T_Scrap_1.ProDate) AND
(Production.[Job #] = T_Scrap_1.[Job #])) INNER JOIN [Job Information Link] ON Production.[Job #] = [Job Information Link].[Job #]) INNER JOIN Machine
ON Production.[Machine #] = Machine.[Machine #]
WHERE (((T_Scrap.ScrapCode) Between "200" And "204")
AND ((T_Scrap_1.ScrapCode)="205")
AND ((Production.[Cast Shots])>0))
GROUP BY Production.[Job #], Year([Production].[ProDate]) & "-" &
WeekNumber([Production].[ProDate]), Machine.Unit;

Nov 12 '05 #4

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

Similar topics

2
3489
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2 attrdata_3 etc, etc...
3
4455
by: james | last post by:
Hi, I would like to get all the records from 9 tables that have the same field value in one field (it is a unique field)that is shared by all the tables. Would this method of joining work: select * from table 1, table 2, table 3 where table 1.com_field = table 2.com_field and table 2.com_field = table 3.com_field
0
1836
by: Gianfranco | last post by:
Hi I got a problem with 2 tables. I have a table, say A, with x records, coming from a make table query and a table, say B, with y records, coming from another make table query. I need to join and link the 2 tables, so that the first record of A is associated with the fist record of B, the second record of A is associated (I mean,in the same...
1
1909
by: Brian | last post by:
I need help joining info from two tables. Table1 and Table 2 have the same fields(Node,Card,Slot,Facility,Sub-Port,Channel,Group,Cic) Group is text, rest are number. Table 1 contains records for all possible combinations of Node,Card,Slot,Facility,Sub-Port,Channel which indicates a physical port on a piece of equipment. Table 2 is imported...
3
9278
by: Reader | last post by:
Hello all, I am joining two tables in a query and the output should be all those records where two fields in table1 match two corresponding fields in table2. I joined the tables using both fields in design view and the Select statement in SQL view looks good. The query runs perfectly and shows the result I want but when I save the query...
12
5536
by: veaux | last post by:
Question about joins in queries. I have 2 tables with a field called "ID". Table 1 Rec1 = Jan12FredFlintstone Rec2 = Feb01WilmaRubble Table 2 Rec1 = Jan12BarneyRubble Rec2 = Mar03SamSlate
2
2593
by: nkechifesie | last post by:
I have created 9 tables in access via visdata and need to create another table joining all these tables. I have used the query in visdata to do this but it gives me an outrageous view. I have entered 2 records in each table but it gives me 65 records in the query. Please could you help me out. This is the code genreated by Visdata after I created...
4
1541
by: Rnt6872 | last post by:
Table A Table B BOL# B_BOL# Chargeback# Hi All, I have been struggling with this for the past few months. I have two tables that I'm inner joining on BOL#=B_BOL#. This works fine. Now for the problem....When there are chargeback#...
2
6408
by: Neekos | last post by:
Im working on web tool that will allow supervisors to see a list of their agents and their call stats for a call center. I have one main table that holds employee IDs and their supervisor names. I then have a dozen other tables that hold many different employee statistics and scores that they accumulate through the year. These tables all have...
0
7178
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
7397
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
7565
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
7128
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5103
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
3255
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...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.