473,405 Members | 2,310 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,405 software developers and data experts.

Trouble getting right data with select query

13
Hello again

I am having some trouble getting the right data out of my database, maybe any of you can help me get the right data.

My Tables:

tblReport
ReportID (pk)
Date

joinTable
ReportID
YardID
JoinID (pk)

tblMachines
MachineID (pk)
Machinename

tblTools
ToolID (pk)
Toolname

ReportMachines
ReportID (pk)
MachineID (pk)

ReportTools
ReportID (pk)
ToolID (pk)

---- And some more tables but these are the most important ones that I have trouble with.

Basically I want to select the tools and machines belonging to each Report made.
When I use this query:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT tblMachines.Machinename, tblTools.Toolname
  2. FROM tblTools INNER JOIN ((tblMachines INNER JOIN (joinTabel INNER JOIN ReportMachines ON joinTabel.JoinID = ReportMachines.ReportID) ON tblMachines.MachineID = ReportMachines.MachineID) INNER JOIN ReportTools ON joinTable.JoinID = ReportTools.ReportID) ON tblTools.ToolID = ReportTools.ToolID
  3. WHERE (((joinTabel.JoinID)=2));
  4.  
The result is:

Machinename - Toolname

Machine1 - Tool1
Machine1 - Tool3
Machine2 - Tool1
Machine2 - Tool3

The result I expected would be:

Machinename - Toolname

Machine1 - Tool1
Machine2 - Tool3

So my database structure is bad or my tables are not correctly joined? Any help is very much appreciated because I am kinda lost in this one.

Thanks for reading already!
Aug 15 '10 #1
7 1472
could you post the few records from each table that lead to the results that you are getting?
Aug 15 '10 #2
NeoPa
32,556 Expert Mod 16PB
I would ask how it can makes sense to use :
Expand|Select|Wrap|Line Numbers
  1. ON joinTabel.JoinID = ReportMachines.ReportID
or even :
Expand|Select|Wrap|Line Numbers
  1. ON joinTable.JoinID = ReportTools.ReportID
Aug 16 '10 #3
Dresse
13
Some records from each table:

tblReport
ReportID (pk) - Date
1 - 7/08/2010
2 - 7/08/2010

joinTable
ReportID - YardID - JoinID (pk)
1 - 1 - 1
2 - 1 - 2

tblMachines
MachineID (pk) - Machinename
1 - Machine1
2 - Machine2
3 - Machine3

tblTools
ToolID (pk) - Toolname
1 - Tool1
2 - Tool2
3 - Tool3


ReportMachines
ReportID (pk) - MachineID (pk)
1 - 1
1 - 2
1 - 3
2 - 1
2 - 2

ReportTools
ReportID (pk) - ToolID (pk)
1 - 1
1 - 2
1 - 3
2 - 1
2 - 3

-------------------------
The above is some testdata, the joinTable is the most important table since i'm want to include it in my WHERE clause.
For example i'm trying to select the machinename and toolname where the joinID equals 1.

@NeoPa

I need to join those tables because I need to know what machines or tools are required for each Report. In ReportMachines or ReportTools each ReportID has a matching MachineID, with that MachineID I can find the right machinename in the other table. Then I try to join the ReportID on the joinTable so I know which Yard and Report it belongs to. I hope my explanation is sufficient enough, its not easy to explain.

Thank you both for reading and I hope you can help me trough this.
I made a small mistake in primary keys in my first post so i updated this now
Aug 16 '10 #4
Dresse:

If I follow this manually, starting with the requirement that JoinID=2 .....

That means that the ReportID will be 2

That means that the Machines in use are 1 and 2
and
The Tools in use are 1 and 3.

Now, the database has no way of knowing which tool goes with which machine, so it mixes them in all possible ways.

Machine 1 with tools 1 and 3; Machine 2 also with tools 1 and 3. Thus the 4 "records" of your original query. You say there should only be 2 rows in the result, but I do not see how that is possible in the current structure.

If there should be a relationship between Machines and Tools, then you need to state it in the database's relationships.

Perhaps you should forget what you already have and start again. Start by stating (in words) what things exist in the real world that your database must track, and how all these "things" are related to each other, and then we can redesign the tables.

I will watch for your reply.

Richard
Aug 17 '10 #5
NeoPa
32,556 Expert Mod 16PB
A point I would re-iterate from my earlier post (although I may not have been as clear as I should have there), as an addition to Richard's very useful post, is that naming objects (tables, fields, etc) can be very important in helping you work things out in your head. Having fields called JoinID & ReportID that are supposed to hold linking data is misleading. It makes it complicated for us to read your SQL, but if anything it's more important that it also makes it much harder for you to follow your own SQL and work with it in any meaningful way.

If the item is a Report ID, then call it that wherever it's found. Join ID is pretty ambiguous. It tells you only that it's used for joining. In a database that's not likely to be unique, so is just noise, leaving you with nothing to indicate what's in the field. Confusion all round.
Aug 17 '10 #6
Dresse
13
I have carefully ready your remarks and adjusted my database and fieldnames accordingly. Thank you very much for the eyeopener. My database is finally working as I was hoping for!

Regards

Dresse
Aug 26 '10 #7
NeoPa
32,556 Expert Mod 16PB
Congratulations indeed then. My points were merely tools to assist, and I'm very glad you found them helpful, but you had to push on from there and fix your SQL using those tools, which is a result :)
Aug 27 '10 #8

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

Similar topics

4
by: projecktzero | last post by:
Well, I've managed to get an image into a postgre database, but now I'm having trouble getting it out. #! /usr/bin/env python from pyPgSQL import PgSQL def main(): connectdb =...
2
by: Marco | last post by:
My SELECT query returns a data set, one column of which contains a set of values corresponding to the same date. I.e. for each date in column "Date", I have a set of numbers in column "Numbers"....
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
3
by: phonl | last post by:
I am a vb6 ADO developer looking at vb.net 2005 and ADO2.net. I used the vb.net 2005 data wizard to bind some controls to a database. Now I want to run a select query and have the bound controls...
1
by: Phil | last post by:
Is it possible to swap rows and columns in select query output so that each record's data is displayed in a column? I want to collect data each day and display it in a query with each day's date...
13
by: angwe23 | last post by:
I've got to fix a system that was basically handed to me. As it currently functions we have four tables as such: 1) Project Hours (includes EmployeeID, ProjectID, Date, Hours) 2) Payrates...
4
by: The.Daryl.Lu | last post by:
Hi, Have a bit of a problem... I've created a form in Access and will use the form for a user to query a table based on the selected fields. The problem lies in that I was using checkboxes for...
2
by: sdbabultarkar | last post by:
Hi I am using below query to retrive the data SELECT RequestDetails FROM Request Where as RequestDetails is Column with Text datatype. I am getting truncated records for...
3
by: josh456 | last post by:
I am having trouble with the following expression in a select query not working. Expr5: IIf(!!="All","All", Or ) If I change the query to the following I am able to retrieve data from my query...
4
by: fran7 | last post by:
Hi, I have a size field where the input is in quotes like 20" x 20" It writes to the database as I see the correct output in the webpage but when I open the record to update it it removes the quotes...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.