Slocky
Slocky

Reputation: 123

Join two or more tables that have no common fields

I have 3 tables in a database

Project              Material Used            Document
------------        -------------------        ------------------------
PID |  Name         MID | PID |  Name          DocID | PID | Date
------------        -------------------        ------------------------
1   | A             1   |  1  |  A1            1     |  1  | 1/1/2016
2   | B             2   |  1  |  A1            2     |  1  | 1/2/2016
3   | C             3   |  1  |  A1            3     |  2  | 1/3/2016
                    4   |  2  |  A1            4     |  2  | 1/4/2016
                    5   |  2  |  A1            5     |  2  | 1/5/2016
                    6   |  3  |  A1            6     |  2  | 1/6/2016
                    7   |  3  |  A1            7     |  2  | 1/7/2016
                    8   |  3  |  A1            8     |  1  | 1/8/2016
                    9   |  3  |  A1            9     |  1  | 1/9/2016

How to query with the results like below ?

------------------------------------------------------------------------
PID     Project Name    MID             Material Name   DocID   Date
------------------------------------------------------------------------
1       A               1               A1              1       1/1/2016
1       A               2               A2              2       1/2/2016
1       A               3               A3              NULL    NULL
2       B               4               B1              3       1/3/2016
2       B               5               B2              4       1/4/2016
2       B               NULL            NULL            5       1/5/2016
2       B               NULL            NULL            6       1/6/2016
2       B               NULL            NULL            7       1/7/2016
3       C               6               C1              8       1/8/2016
3       C               7               C2              9       1/9/2016
3       C               8               C3              NULL    NULL
3       C               9               C4              NULL    NULL

PID in Material and Document table is Foreign Key.

I am using Microsoft SQL 2008. Is it possible ?

Upvotes: 0

Views: 513

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271003

You seem to want lists in the columns. You can get this using full outer join and row_number():

select p.*, m.mid, m.name, d.docid, d.date 
from project p left join
     (select m.*, row_number() over (partition by pid order by mid) as seqnum
      from materials
     ) m
     on p.pid = m.pid full outer join
     (select d.*, row_number() over (partition by pid order by docid) as seqnum
      from documents
     ) d
     on p.pid =  d.pid and m.seqnum = d.seqnum;

Hmmm, try this version:

select p.*, md.mid, md.name, md.docid, md.date 
from project p left join
     (select m.id, m.name, d.docid, d.date
      from (select m.*, row_number() over (partition by pid order by mid) as seqnum
            from materials
           ) m full outer join
           (select d.*, row_number() over (partition by pid order by docid) as seqnum
            from documents
           ) d
           on p.pid =  d.pid and m.seqnum = d.seqnum
      ) md
     on p.pid = md.pid;

Upvotes: 1

Related Questions