Solved

SQL Query - relational

Posted on 2011-03-14
3
338 Views
Last Modified: 2012-05-11
I have a table PairIDs with columns FirstID and SecondID
FirstID            SecondID
120      6098
121                  9192


I have another table MainID with columns MainID, MainPairID
MainID           MainPairID
800812            120
800992            121
900782            6098
900324             9192


I have another table CompletionID with columns MainID, CompletionNumber
MainID   CompletionNumber
800812                3
800992                4
900782                5
900324                6


I need a query to display
PairID.FirstID, PairID.SecondID, MainID(for PairID.FirstID), MainID(for PairID.SecondID), CompletionNumber( for PairID.FirstID), CompletionNumber(for PairID.SecondID)

As Follows:
FirstID SecondID MainID      Main_ID      CompletionNumber      CompNo
120    6098              800812      900782         3                                        5
121    9192              800992      900324         4                6


JOINS:
MainID inner join CompletionID on MainID.MainID = CompletionID.MainID

PairID inner join MainID on PairID.FirstID

PairID inner join MainID on PairID.SecondID
0
Comment
Question by:spirose
  • 2
3 Comments
 
LVL 22

Expert Comment

by:8080_Diver
ID: 35131337
Try the Attached.  Note: I used left outer joins in places because I don't know if the data may or may not exist.
SELECT P.FirstID
  ,P.SecondID
  ,P1.MainID
  ,P2.MainID
  ,P1.CompletionID
  ,P2.CompletionID
FROM PairIDs P
LEFT OUTER JOIN
    (SELECT P.FirstID
      ,M.MainID
      ,C.CompletionNumber
    FROM PairIDs P
    INNER JOIN MainID M
    ON  M.MainPairID = P.SecondID
    LEFT OUTER JOIN CompletionID C
    ON  M.MainID = C.MainID
    ) P1
ON  P.FirstID = P1.FirstID
LEFT OUTER JOIN
    (SELECT P.SecondID
      ,M.MainID
      ,C.CompletionNumber
    FROM PairIDs P
    INNER JOIN MainID M
    ON  M.MainPairID = P.SecondID
    LEFT OUTER JOIN CompletionID C
    ON  M.MainID = C.MainID
    ) P2
ON  P.SecondID = P2.SecondID;

Open in new window

0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35131349
try this query.
select p.FirstID,p.SecondID,m1.MainID,m2.MainID Main_ID,c1.CompletionNumber,c2.CompletionNumber CompNo
  from PairIDs p
  join MainIDs m1 on p.FirstID = m1.MainPairID
  join MainIDs m2 on p.SecondID = m2.MainPairID
  join CompletionID c1 on m1.MainID = c1.MainID
  join CompletionID c2 on m2.MainID = c2.MainID

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 35131359
you can use LEFT JOIN if data is not available in other tables.
select p.FirstID,p.SecondID,m1.MainID,m2.MainID Main_ID,c1.CompletionNumber,c2.CompletionNumber CompNo
  from PairIDs p
  left join MainIDs m1 on p.FirstID = m1.MainPairID
  left join MainIDs m2 on p.SecondID = m2.MainPairID
  left join CompletionID c1 on m1.MainID = c1.MainID
  left join CompletionID c2 on m2.MainID = c2.MainID

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

708 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