Frage

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.

War es hilfreich?

Lösung

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;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top