Aan
Aan

Reputation: 12890

Modifing SQL statement to append a column from a table

I have two tables: TrainingMatrix and Data. For TrainingMatrix I have this SQL statement:

SELECT DISTINCT ON (payroll, "TrainingName", "Institute") *
            FROM   "TrainingMatrix" 
            ORDER  BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST;

Each payroll No. is related to a unique employee name. So, in Data table I have two columns: payroll and "EmployeeName". How can I modify the previous statement such that I can show/project "EmployeeName" with the result as well.

I use PostgreSQL 9.2 and pgAdmin III.

Upvotes: 1

Views: 75

Answers (1)

Mureinik
Mureinik

Reputation: 311188

You can join your query with the Data table:

SELECT DISTINCT ON ("TrainingMatrix".payroll, "TrainingName", "Institute") "Data".EmployeeName, "TrainingMatrix".*
FROM "TrainingMatrix" 
JOIN "Data" ON "TrainingMatrix".payroll = "Data".payroll
ORDER  BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST;

Upvotes: 1

Related Questions