473,507 Members | 2,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble with one to many relationship in single SQL query

Plater
7,872 Recognized Expert Expert
(I don't even know how to title this)
The bossman says I need to add a certain field into a query and I have yet to figure out out to do it.
I will try and make a trimmed down example.


Table1's columns:
Expand|Select|Wrap|Line Numbers
  1. myUniqueIDField | (other columns) 
  2.  
Table2's columns:
Expand|Select|Wrap|Line Numbers
  1. aUniqueID | LinkToTable1myUniqueIDField | StringColumn | (other columns)
  2.  
So what I'm saying is: Any entry in Table1 can have 0-n number of entries in Table2. Nothing special there.
I need ALL columns from all entries in Table1, regardless of if they are in Table2 (left join, nothing big yet)
The problem comes with the extra column he wants.
The contents of that extra column need to be dependant on what is found in Table2 and based on Table2's [StringColumn].

For example some entries in Table2 code be:
Expand|Select|Wrap|Line Numbers
  1. 1  234  "Action Needed"
  2. 2  367  "Action Taken"
  3. 3  234  "Bad Data"
  4.  
My computed column needs to know if for a given record of Table1, if there are any entries in Table2 that say "Action Needed" or "Action Taken".
The computed column (for all it matters) could have three possible values then:
It could be blank (meaning it found nothing of use in Table2)
It could say "Action Needed" (meaning it found an entry in Table2 that matches)
It could say "Action Taken" (meaning it found an entry in Table2 that matches)

If this were a one - to - one relationship it would be easy enough to be like Table2.StringColumn and just display it's contents, but there will be many entries in Table2.

So if anyone made any sense of that and has any suggestions I'm all ears.
(It would even be acceptable to me if the extra column contained a concatinated string of all the StringColumns in Table2 that matched that record, I could post process)
Feb 20 '08 #1
6 3187
JovieUrbano
8 New Member
Am confused with all those lines.. please make a display of your desired output.. :)
Feb 20 '08 #2
Plater
7,872 Recognized Expert Expert
Well maybe this sample set will help make things more understandable. It's hard to explain.

Sample Table1 Data:
Expand|Select|Wrap|Line Numbers
  1. 200 (some data)
  2. 201 (some data)
  3. 202 (some data)
  4. 203 (some data)
  5. 204 (some data)
  6. 205 (some data)
  7.  
Sample Table2 Data:
Expand|Select|Wrap|Line Numbers
  1. 1   200   "SomeValue"
  2. 2   200   "SomeOtherValue"
  3. 3   200   "Action Required"
  4. 4   202   "Acion Taken"
  5. 5   202   "SomeValue"
  6. 6   203   "SomeValue"
  7. 7   204   "Action Required"
  8.  
Sample Output of the query:
Expand|Select|Wrap|Line Numbers
  1. 200   (some data)   "Action Required"
  2. 201   (some data)   ""
  3. 202   (some data)   "Action Taken"
  4. 203   (some data)   ""
  5. 204   (some data)   "Action Required"
  6. 204   (some data)   ""
  7.  
Feb 20 '08 #3
ck9663
2,878 Recognized Expert Specialist
Well maybe this sample set will help make things more understandable. It's hard to explain.

Sample Table1 Data:
Expand|Select|Wrap|Line Numbers
  1. 200 (some data)
  2. 201 (some data)
  3. 202 (some data)
  4. 203 (some data)
  5. 204 (some data)
  6. 205 (some data)
  7.  
Sample Table2 Data:
Expand|Select|Wrap|Line Numbers
  1. 1   200   "SomeValue"
  2. 2   200   "SomeOtherValue"
  3. 3   200   "Action Required"
  4. 4   202   "Acion Taken"
  5. 5   202   "SomeValue"
  6. 6   203   "SomeValue"
  7. 7   204   "Action Required"
  8.  
Sample Output of the query:
Expand|Select|Wrap|Line Numbers
  1. 200   (some data)   "Action Required"
  2. 201   (some data)   ""
  3. 202   (some data)   "Action Taken"
  4. 203   (some data)   ""
  5. 204   (some data)   "Action Required"
  6. 204   (some data)   ""
  7.  
Llet's see. you need all the rows and columns in table1 regardless if the key exists in table 2. You also need the status (am assuming this column name) in table2 if the key is existing in table2 and the status has the word 'action' on it.

If am wrong, don't continue reading this.

Otherwise, try something like...

Expand|Select|Wrap|Line Numbers
  1. select table1.*, table2.someothercolumn, 
  2. from table1 left join table2 on table1.key = table2.key
  3. left join (select key, somecolumn, statusoftask from table2 where statusoftask like '%action%') status on status.key = table1.key
  4.  
If you have two or more status with the word 'action' on it, it will grab the first one. If there are no status with the word 'action', it'll be NULL. just use IsNull as necessary.

Good luck,

-- CK
Feb 20 '08 #4
Plater
7,872 Recognized Expert Expert
That looks like what I am after.
I was not aware I could use a select clause like that in the FROM section.
I will have to investigate it when back at work tomorrow and let you know if it works out. Thanks.
Feb 20 '08 #5
ck9663
2,878 Recognized Expert Specialist
Llet's see. you need all the rows and columns in table1 regardless if the key exists in table 2. You also need the status (am assuming this column name) in table2 if the key is existing in table2 and the status has the word 'action' on it.

If am wrong, don't continue reading this.

Otherwise, try something like...

Expand|Select|Wrap|Line Numbers
  1. select table1.*, table2.someothercolumn, 
  2. from table1 left join table2 on table1.key = table2.key
  3. left join (select key, somecolumn, statusoftask from table2 where statusoftask like '%action%') status on status.key = table1.key
  4.  
If you have two or more status with the word 'action' on it, it will grab the first one. If there are no status with the word 'action', it'll be NULL. just use IsNull as necessary.

Good luck,

-- CK

Also, try:


Expand|Select|Wrap|Line Numbers
  1. select table1.*, table2.someothercolumn, stat.statusoftask 
  2. from table1 
  3. left join table2 on table1.key = table2.key
  4. left join table2 stat on table1.key = stat.key and  statusoftask like '%action%'
  5.  
If this works, try this one coz I think this is faster, because there's no subquery. You might also want to create index for faster performance.

I also think I might've missed a column. It should've been:

Expand|Select|Wrap|Line Numbers
  1. select table1.*, table2.someothercolumn, stat.statusoftask 
  2. from table1 left join table2 on table1.key = table2.key
  3. left join (select key, somecolumn, statusoftask from table2 where statusoftask like '%action%') status on status.key = table1.key
  4.  
Happy coding

-- CK
Feb 21 '08 #6
Plater
7,872 Recognized Expert Expert
Oh yeah, that 2nd one using LIKE works great.
Learning all kinds of new things. Was unaware the JOINs could have more then one "requirement" thing in there. Thought it could only have the key=key thing.

Thanks for the help on that. I had been trying like group by and distinct but wasn't getting anywhere.
Feb 22 '08 #7

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

Similar topics

3
12898
by: kj | last post by:
When I run the attached query, I get duplicates when there is one to many relationship between tableA and tableB. The query, tested schema and the result is attached. Sorry for the long post. ...
9
13702
by: Simon Withers | last post by:
I have 3 data tables, A, B and C, with many to many relationship tables between A-B and A-C. The data in A and C changes rarely, and the A-C relationship relates all possible combinations of A...
2
908
by: Keith | last post by:
I am having a problem creating a many-to-many-to-many type relationship. It works fine, but when I create a view to query it and test it, it does not generate the results I expected. Below...
2
2672
by: Megan | last post by:
hello everybody, i know this is a very long post, but i wanted to provide as much detail as possible. quick overview- i want to create a couple of many to many relationships and am wondering...
4
2062
by: Apple | last post by:
I have to create a query with many to many relationship, but I can't break it into 2 x 1 to many, should there anyone can teach me how to solve this problem. Thanks in advance!
59
7433
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
5
1276
by: pedro8ae | last post by:
Hi friends, I am developing a project for an department in a school. this school has some other sub-campus and each campus has different rooms. ex.: School XY: sub-Campus 1:-Room1.1, Room 1.2,...
1
1654
by: Tableshavturned | last post by:
Hi this is my first post on the forums. I haven't really developed before with Access 2003 so trouble shooting with this application is not my forte. The issue at hand is, created a star schema with...
6
4047
by: NicoleCartrette | last post by:
Going back to school is easier said than done.. This was posted to an older thread earlier but I don't think it got any attention. Your help is appreciated Professor requires we create a...
0
7111
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...
0
7319
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
7376
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...
1
7031
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
7485
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...
0
5623
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
1542
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 ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
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...

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.