2013-10-24 90 views
1

我有兩個表:TrainingMatrixData。對於TrainingMatrix我有這樣的SQL語句:修改SQL語句以從表中追加一列

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

每個工資號是關係到一個唯一的僱員姓名。所以,在Data表中我有兩列:payroll"EmployeeName"。我如何修改前面的陳述,以便我可以顯示/項目"EmployeeName"以及結果。

我使用PostgreSQL 9.2和pgAdmin III。

回答

1

你可以加入與Data表查詢:

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; 
+0

感謝親愛的,你的意思是'不Data''Fata' :)。還有一件事我怎麼能把'EmployeeName'作爲第一列顯示? – Aan

+0

感謝您的注意,@Aan。我已經修復了我的答案,以解決您的更正和其他問題。 – Mureinik