Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

T SQL Query for 2 tables with one to many relationship

Posted on 2011-09-23
8
Medium Priority
?
300 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

885 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