Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

T SQL Query for 2 tables with one to many relationship

Posted on 2011-09-23
8
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 600 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 1400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

670 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