我使用以下SQL语句预测了几列:
SELECT DISTINCT ON ("TrainingMatrix".payroll, "TrainingName", "Institute")"gendata"."Employee Name","gendata"."Position", "gendata"."Department", "TrainingMatrix".* "
"FROM "TrainingMatrix" "
"JOIN "gendata" ON "TrainingMatrix".payroll = "gendata".payroll "
"ORDER BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST
我想根据"TrainingMatrix".payroll
值过滤结果。我尝试了以下但它显示错误:
SELECT * (SELECT DISTINCT ON ("TrainingMatrix".payroll, "TrainingName", "Institute")"gendata"."Employee Name","gendata"."Position", "gendata"."Department", "TrainingMatrix".* "
"FROM "TrainingMatrix" "
"JOIN "gendata" ON "TrainingMatrix".payroll = "gendata".payroll "
"ORDER BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST) WEHRE "TrainingMatrix".payroll='40612010' ;"
我收到了这个错误:
ERROR: 42601: syntax error at or near "("
我正在使用PostgreSQL。
修改 的
SELECT * from (SELECT DISTINCT ON ("TrainingMatrix".payroll, "TrainingName", "Institute")"gendata"."Employee Name","gendata"."Position", "gendata"."Department", "TrainingMatrix".*
FROM "TrainingMatrix"
JOIN "gendata" ON "TrainingMatrix".payroll = "gendata".payroll
ORDER BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST) where "TrainingMatrix".payroll='40612010' ;
现在我得到了:
错误:FROM中的子查询必须有一个别名LINE 1:SELECT * from (SELECT DISTINCT ON(“TrainingMatrix”.payroll ...
答案 0 :(得分:1)
你的子查询需要一个from,而你在那里拼错了。
编辑:并添加别名
SELECT * from (SELECT DISTINCT ON ("TrainingMatrix".payroll, "TrainingName", "Institute")"gendata"."Employee Name","gendata"."Position", "gendata"."Department", "TrainingMatrix".* "
^^^^
"FROM "TrainingMatrix" "
"JOIN "gendata" ON "TrainingMatrix".payroll = "gendata".payroll "
"ORDER BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST) a where "TrainingMatrix".payroll='40612010' ;"
^ ^^^^^