简体   繁体   中英

Combining Multiple tables in Query (SQL, MS Access)

I need to query 3 tables and get data into one table.

Complexity_Matrix - (`Project`, `Function`, `Complexity`)
Project_Phase_Selection - (`Project`, `Month`, `Phase`)
Complexity_Data_Sheet - (`Function`, `Complexity`, `Phase`, `Needed`)

Complexity_Matrix - A Project has some 10 functions and each function is at different complexity level for a project Project_Phase_Selection - Monthwise Project Phase Complexity_Data_Sheet - Input sheet for resource requirement based on Function, Complexity and phase.

CREATE TABLE Complexity_Matrix
    (`Project` varchar(31), `Function` varchar(10), `Complexity` varchar(6))
;

INSERT INTO Complexity_Matrix
    (`Project`, `Function`, `Complexity`)
VALUES
    ('3D Templates - Project DeLorean', 'Clinical', 'Low'),
    ('3D Templates - Project DeLorean', 'GSM', 'Medium'),
    ('3D Templates - Project DeLorean', 'HEMA', 'Low'),
    ('3D Templates - Project DeLorean', 'Medical', 'Medium'),
    ('3D Templates - Project DeLorean', 'PJM', 'Low'),
    ('3D Templates - Project DeLorean', 'Quality', 'Medium'),
    ('3D Templates - Project DeLorean', 'R&D', 'Low'),
    ('3D Templates - Project DeLorean', 'Regulatory', 'Medium'),
    ('3D Templates - Project DeLorean', 'SC', 'Medium'),
    ('3D Templates - Project DeLorean', 'Sourcing', 'Medium'),
    ('4.5mm Distal Femur Plate (VET)', 'Clinical', 'Low'),
    ('4.5mm Distal Femur Plate (VET)', 'GSM', 'Low'),
    ('4.5mm Distal Femur Plate (VET)', 'HEMA', 'Low'),
    ('4.5mm Distal Femur Plate (VET)', 'Medical', 'Low'),
    ('4.5mm Distal Femur Plate (VET)', 'PJM', 'Low'),
    ('4.5mm Distal Femur Plate (VET)', 'Quality', 'Low'),
    ('4.5mm Distal Femur Plate (VET)', 'R&D', 'Low'),
    ('4.5mm Distal Femur Plate (VET)', 'Regulatory', 'Low'),
    ('4.5mm Distal Femur Plate (VET)', 'SC', 'Low'),
    ('4.5mm Distal Femur Plate (VET)', 'Sourcing', 'Low')
;


CREATE TABLE Project_Phase_Selection
    (`Project` varchar(31), `Month` datetime, `Phase` varchar(1))
;

INSERT INTO Project_Phase_Selection
    (`Project`, `Month`, `Phase`)
VALUES
    ('3D Templates - Project DeLorean', '2019-01-01 00:00:00', '3'),
    ('3D Templates - Project DeLorean', '2019-02-01 00:00:00', '3'),
    ('3D Templates - Project DeLorean', '2019-03-01 00:00:00', '3'),
    ('3D Templates - Project DeLorean', '2019-04-01 00:00:00', '3'),
    ('3D Templates - Project DeLorean', '2019-05-01 00:00:00', '3'),
    ('3D Templates - Project DeLorean', '2019-06-01 00:00:00', '3'),
    ('3D Templates - Project DeLorean', '2019-07-01 00:00:00', '3'),
    ('3D Templates - Project DeLorean', '2019-08-01 00:00:00', '4'),
    ('3D Templates - Project DeLorean', '2019-09-01 00:00:00', '4'),
    ('3D Templates - Project DeLorean', '2019-10-01 00:00:00', '4'),
    ('3D Templates - Project DeLorean', '2019-11-01 00:00:00', '4'),
    ('3D Templates - Project DeLorean', '2019-12-01 00:00:00', '4'),
    ('4.5mm Distal Femur Plate (VET)', '2019-01-01 00:00:00', 'C'),
    ('4.5mm Distal Femur Plate (VET)', '2019-02-01 00:00:00', 'C'),
    ('4.5mm Distal Femur Plate (VET)', '2019-03-01 00:00:00', 'C'),
    ('4.5mm Distal Femur Plate (VET)', '2019-04-01 00:00:00', '1'),
    ('4.5mm Distal Femur Plate (VET)', '2019-05-01 00:00:00', '1'),
    ('4.5mm Distal Femur Plate (VET)', '2019-06-01 00:00:00', '1'),
    ('4.5mm Distal Femur Plate (VET)', '2019-07-01 00:00:00', '2'),
    ('4.5mm Distal Femur Plate (VET)', '2019-08-01 00:00:00', '2'),
    ('4.5mm Distal Femur Plate (VET)', '2019-09-01 00:00:00', '2'),
    ('4.5mm Distal Femur Plate (VET)', '2019-10-01 00:00:00', '2'),
    ('4.5mm Distal Femur Plate (VET)', '2019-11-01 00:00:00', '2'),
    ('4.5mm Distal Femur Plate (VET)', '2019-12-01 00:00:00', '3')
;

CREATE TABLE Complexity_Data_Sheet
    (`Function` varchar(8), `Complexity` varchar(6), `Phase` varchar(1), `Needed` int)
;

INSERT INTO Complexity_Data_Sheet
    (`Function`, `Complexity`, `Phase`, `Needed`)
VALUES
    ('Clinical', 'Low', 'A', 0),
    ('Clinical', 'Low', 'B', 0),
    ('Clinical', 'Low', 'C', 0.05),
    ('Clinical', 'Low', '1', 0.2),
    ('Clinical', 'Low', '2', 0.3),
    ('Clinical', 'Low', '3', 0.5),
    ('Clinical', 'Low', '4', 0.5),
    ('Clinical', 'Low', '5', 0.1),
    ('Clinical', 'Low', '6', 0.05),
    ('Clinical', 'Medium', 'A', 0),
    ('Clinical', 'Medium', 'B', 0),
    ('Clinical', 'Medium', 'C', 0.1),
    ('Clinical', 'Medium', '1', 0.4),
    ('Clinical', 'Medium', '2', 0.6),
    ('Clinical', 'Medium', '3', 0.8),
    ('Clinical', 'Medium', '4', 0.8),
    ('Clinical', 'Medium', '5', 0.2),
    ('Clinical', 'Medium', '6', 0.05),
    ('Clinical', 'high', 'A', 0),
    ('Clinical', 'high', 'B', 0),
    ('Clinical', 'high', 'C', 0.2),
    ('Clinical', 'high', '1', 1),
    ('Clinical', 'high', '2', 1.5),
    ('Clinical', 'high', '3', 2.5),
    ('Clinical', 'high', '4', 2),
    ('Clinical', 'high', '5', 0.5),
    ('Clinical', 'high', '6', 0.1)
;

I Tried the Following:

SELECT distinct Complexity_Matrix.Project, Project_Phase_Selection.Month, Project_Phase_Selection.Phase,Complexity_Matrix.Function, Complexity_Matrix.Complexity, Complexity_Data_Sheet.Needed FROM (Project_Phase_Selection INNER JOIN Complexity_Data_Sheet ON Project_Phase_Selection.[Phase] = Complexity_Data_Sheet.[Phase]) INNER JOIN Complexity_Matrix ON (Complexity_Data_Sheet.[Complexity] = Complexity_Matrix.[Complexity]) AND (Complexity_Data_Sheet.[Function] = Complexity_Matrix.[Function]) ORDER BY Complexity_Matrix.Function, Complexity_Matrix.Project, Complexity_Matrix.Function, Complexity_Matrix.Complexity, Project_Phase_Selection.Month, Project_Phase_Selection.Phase;

So I need to query these 3 tables and need a Result like Project, Function,Month,Phase,Complexity,Needed

only for all the matched columns values and the result column is needed.

But i'm getting all the values for all fields instead of distinct values. Please help

You can try this code:

select CM.Project, CM.function,PPS.Month,PPS.Phase,CDS.Complexity,CDS.Needed
from Complexity_Matrix CM
INNER JOIN Project_Phase_Selection PPS on CM.[Project]=PPS.[project]
INNER JOIN Complexity_Data_Sheet CDS on CM.[function]=CDS.[function] and CM.[Complexity]=CDS.[Complexity] and PPS.[Phase]=CDs.[Phase]
group by CM.Project, CM.function,PPS.Month,PPS.[Phase],CDS.Complexity,CDS.Needed

Demo

SELECT DISTINCT CM.Project, CM.function, PPS.Month, PPS.Phase, CDS.Complexity, CDS.Needed
FROM (Complexity_Matrix AS CM 
INNER JOIN Project_Phase_Selection AS PPS ON CM.[Project]=PPS.[project]) 
INNER JOIN Complexity_Data_Sheet AS CDS ON (CM.[Complexity]=CDS.[Complexity] ) AND (CM.[function]=CDS.[function]) AND (PPS.[Phase]=CDS.[Phase])
GROUP BY CM.Project, CM.function, PPS.Month, PPS.[Phase], CDS.Complexity, CDS.Needed;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM