过滤SELECT语句的投影

时间:2013-11-10 11:30:43

标签: sql database postgresql

我使用以下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 ...

1 个答案:

答案 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' ;"
                                                                                             ^ ^^^^^