473,666 Members | 2,039 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.Pro Date =
T_Scrap.ProDate ) AND (Production.[Job #] = T_Scrap.[Job #])) LEFT JOIN
T_Scrap AS T_Scrap_1 ON (Production.Pro Date = T_Scrap_1.ProDa te) 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.Scra pCode) Between "200" And "204")
AND ((T_Scrap_1.Scr apCode)="205")
AND ((Production.[Cast Shots])>0))
GROUP BY Production.[Job #], Year([Production].[ProDate]) & "-" &
WeekNumber([Production].[ProDate]), Machine.Unit;
Nov 12 '05 #1
3 1706
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.Pro Date =
T_Scrap.ProDate ) AND (Production.[Job #] = T_Scrap.[Job #])) LEFT JOIN
T_Scrap AS T_Scrap_1 ON (Production.Pro Date = T_Scrap_1.ProDa te) 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.Scra pCode) Between "200" And "204")
AND ((T_Scrap_1.Scr apCode)="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.Scra pCode) Between "200" And "204")
AND ((T_Scrap_1.Scr apCode)="205")
is it.and mist likely the
AND ((T_Scrap_1.Scr apCode)="205")
line.
Maybe it needs to be
WHERE (((T_Scrap.Scra pCode) Between "200" And "204")
AND (T_Scrap_1.Scra pCode="205" Or T_Scrap_1.Scrap Cod 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******** ************@ne ws20.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.Pro Date =
T_Scrap.ProDate ) AND (Production.[Job #] = T_Scrap.[Job #])) LEFT JOIN
T_Scrap AS T_Scrap_1 ON (Production.Pro Date = T_Scrap_1.ProDa te) 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.Scra pCode) Between "200" And "204")
AND ((T_Scrap_1.Scr apCode)="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
3503
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
4469
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
1841
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 row) with the second record of B and so on...until the last row of A (where x<y). I tried to...
1
1916
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 from a piece of equipment containing all above info that has an assigned Group and Cic. I want to...
3
9291
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 and close it, re-opening in design view shows the error message "Microsoft Office Access can't
12
5551
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
2596
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 the query, please what is wrong. the common field in 8 of the tables is Vehicle no and the common...
4
1555
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# fields associated with B_BOL# they aren't being captured as additional records. None of my tables
2
6417
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 employee IDs in them, but the IDs would reoccur everyday (so they are not unique) and there is data...
0
8356
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8783
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8640
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2773
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
2
1776
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.