Solved

SQL Query - relational

Posted on 2011-03-14
3
366 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Dump exec output to table 3 21
Acces SQL Insert Problem 6 31
SQL View / Qtry 3 10
MS SQL AND PASSING A TABLE NAME TO A SPROC 5 14
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Backup & Restore 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 set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

856 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