Solved

T SQL Query for 2 tables with one to many relationship

Posted on 2011-09-23
8
281 Views
Last Modified: 2012-05-12
Hi Experts,

I need to query 2 tables that have a one to many relationship. Following are the tables:

tableA
*********
processID                 raceID                        raceName  
-------------               ---------                       -----------------------------------
6676                         34                               Men's 400 m relay - High School
6677                         102                             Junior girls' 100 m - Middle School
6678                          9987                          Boy's 100 m championship - Elem    

tableB
*********
processID         competeDate
-------------      -----------------------------------
6676                9/20/2011 1:45:40  
6676                9/20/2011 2:32:40
6678                9/20/2011 1:15:25
6678                9/21/2011 2:15:25
6677                8/20/2011 2:32:40
6677                8/20/2011 8:45:40
6677                8/20/2011 9:45:40    

As you can see, tableA has a one to many relationship to tableB. I need 2 queries, one to get all data for one processID and another to get all processID's, both sorted by date-time and second one by processID and date-time. This is what I need my data to look like:

1st query data:
-------------------
processID       raceID       raceName                                              competeDate
-------------     ---------      -----------------------------------                ------------------------
6676              34              Men's 400 m relay - High School            9/20/2011 1:45:40
6676              34              Men's 400 m relay - High School            9/20/2011 2:32:40

2nd query data:
--------------------
processID       raceID       raceName                                              competeDate
-------------     ---------      -----------------------------------                ------------------------
6676              34              Men's 400 m relay - High School            9/20/2011 1:45:40
6676              34              Men's 400 m relay - High School            9/20/2011 2:32:40
6677              102           Junior girls' 100 m - Middle School          8/20/2011 2:32:40
6677              102           Junior girls' 100 m - Middle School          8/20/2011 8:45:40
6677              102           Junior girls' 100 m - Middle School          8/20/2011 9:45:40
6678              9987         Boy's 100 m championship - Elem           9/20/2011 1:15:25
6678              9987         Boy's 100 m championship - Elem           9/21/2011 2:15:25

How would I need to write my 2 queries?
 
0
Comment
Question by:roger_v
  • 3
  • 3
  • 2
8 Comments
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 150 total points
ID: 36591236
First Query:
SELECT A.*, B.competeDate
FROM
	tableA As A INNER JOIN
	tableB As B ON A.processID=B.processID
WHERE A.processID=6676
ORDER BY B.competeDate

Open in new window

Second Query:
SELECT A.*, B.competeDate
FROM
	tableA As A INNER JOIN
	tableB As B ON A.processID=B.processID
ORDER BY A.processID,B.competeDate

Open in new window

Here's a good site to start learning SQL: http://www.w3schools.com/sql/sql_join_inner.asp
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 350 total points
ID: 36591237
Seems like the same query would do both, just the first needs a processID restriction. Try:
declare @ProcessId varchar(38)

--set @ProcessId = 6676

select processID, raceID, raceName, competeDate
from tableA A, tableB B
where B.processID = A.processID
and (@ProcessId is null or A.processID = @ProcessId)
order by A.processId, B.competeDate

Open in new window

0
 
LVL 1

Author Comment

by:roger_v
ID: 36591270
@Thomas & @dale:

To do an order by, can I use the column number? For example, if I want to sort by competeDate and it is column 6, wouldn't an 'order by 6' work? I tried it but it gives me an error.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Author Comment

by:roger_v
ID: 36591272
Never mind, it worked. It gave me a 'transport level error', but now it works.
0
 
LVL 22

Expert Comment

by:Thomasian
ID: 36591274
Yes it should work. The order number should be based on the order in the SELECT statement. In the query (we provided), there are only 4 columns so to sort by competeDate you should use ORDER BY 4
0
 
LVL 1

Author Comment

by:roger_v
ID: 36591279
@dale:

Your query as is, gave the 2nd query result set which is fine. To get the 1st query result set, do I need to have an additional 'AND' in the where clause?

Also, I don't understand this part:

(@ProcessId is null or A.processID = @ProcessId)

0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 36591280
You learn something new everyday!

But I wouldn't do it that way, a) its not immediately obvious which column is being used and b) if you make any changes to the columns it could change the sort order.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 36591283
Thats so you can use the same query for both situations, you either set @ProcessId or leave it null. To make the first query work all you need to do is uncomment the line starting -- set @ProcessId...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now