Solved

T SQL Query for 2 tables with one to many relationship

Posted on 2011-09-23
8
279 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

10 Experts available now in Live!

Get 1:1 Help Now