Reputation: 12890
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
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