Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL Select Join 3 Tables

I'm currently working on a select query in T-SQL on SQL Server 2012. It's a complex query, I want to query a list from 3 tables. The result should look something like this:

Desired Output:

ProjectId |    Title    | Manager   | Contact   | StatusId 
----------+-------------+-----------+-----------+-----------
1         |   projectX  |   1123    |  4453     |  1 
2         |   projectY  |   2245    |  5567     |  1
3         |   projectZ  |   3335    |  8899     |  1

My 3 Tables:

1) Project: ProjectId, ProjectDataId, MemberVersionId
2) ProjectData: ProjectDataId, Title, StatusId
3) Members: MemberId, MemberVersionId, MemberTypeId, EmployeeId

The tricky part is, to implement versioning. Thus, over time the project Members can change, and it should always be possible to return to a previous version, that's why I use MemberVersionId as a foreign key inbetween Project and Members. The tables Project and ProjectData a linked with ProjectDataId.

Hence, 1 Project has 1 OfferData and 1 Project has N Members.

Some sample data:
Project

ProjectId | ProjectDataId | MemberVersionId | 
----------+---------------+-----------------+
1         |   2           |   1             | 
2         |   3           |   1             |
3         |   4           |   1             |

ProjectData

ProjectDataId |    Title    | StatusId 
--------------+-------------+-----------
2             |   projectX  |  1 
3             |   projectY  |  1
4             |   projectZ  |  1

Members: MemberTypeId 1 = Manager, MemberTypeId 2 = Contact, 3 = Other

MemberId | MemberVersionId | MemberTypeId | EmployeeId | 
---------+-----------------+--------------+------------+
1        |   1             |   1          |  1123      | 
2        |   1             |   2          |  4453      |
3        |   1             |   3          |  9999      |
4        |   2             |   1          |  2245      | 
5        |   2             |   2          |  5567      | 
6        |   2             |   3          |  9999      | 
7        |   3             |   1          |  3335      | 
8        |   3             |   2          |  8899      | 
9        |   3             |   3          |  9999      | 

My current query looks like this:

SELECT ProjectId, Title, EmployeeId AS Manager, EmployeeId AS Contact, StatusId
FROM [MySchema].[Project] a,
     [MySchema].[ProjectData] b,
     [MySchema].[Members] c
WHERE a.ProjectDataId = b.ProjectDataId
  AND a.MemberVersionId = c.MemberVersionId

Unfortunately this doesn't work yet. Do you know how to solve this issue?

Thanks

like image 537
TimHorton Avatar asked Dec 03 '22 13:12

TimHorton


1 Answers

Something like this?

SELECT 
    p.ProjectId, 
    pd.Title, 
    mm.EmployeeId AS Manager, 
    mc.EmployeeId AS Contact, 
    pd.StatusId
FROM 
    [MySchema].[Project] p
    INNER JOIN [MySchema].[ProjectData] pd ON pd.ProjectDataId = p.ProjectDataId
    INNER JOIN [MySchema].[Members] mm ON mm.MemberVersionId = p.MemberVersionId AND mm.MemberTypeId = 1
    INNER JOIN [MySchema].[Members] mc ON mc.MemberVersionId = p.MemberVersionId AND mc.MemberTypeId = 2;
like image 123
Richard Hansell Avatar answered Dec 24 '22 23:12

Richard Hansell