我编写了以下SQL语句来从两个表gendata
&中获取数据。 TrainingMatrix
:
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) AS foo;
它工作正常,但我需要通过以下方式过滤记录:
WHERE "TrainingMatrix"."ExpiryDate" - current_date <= 0
AND EXTRACT(YEAR FROM "TrainingMatrix"."ExpiryDate") = EXTRACT(YEAR FROM current_date);
因此,原始SQL语句将是:
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) AS foo WHERE "TrainingMatrix"."ExpiryDate" - current_date <= 0
AND EXTRACT(YEAR FROM "TrainingMatrix"."ExpiryDate") = EXTRACT(YEAR FROM current_date);
但我收到了这个错误:
错误:表“TrainingMatrix”LINE 3缺少FROM子句条目: ...... te“,”TrainingDate“DESC NULLS LAST)AS Foo WHERE”TrainingM ...
我正在使用PostgreSQL。任何建议的人?
答案 0 :(得分:7)
100%@a_horse already said。还有几件事:
格式化您的查询,以便在您尝试调试之前很容易阅读和理解。在您在公共论坛上发帖之前更是如此。
使用table aliases,特别是使用您不幸的CaMeL案例名称,以便于阅读。
在查询中提供您的表格定义或至少 table-qualify 列名称,以便我们有机会解析它。您的直接问题已在下面的查询中修复。您还可以相应地替换?.
:
t
.. "TrainingMatrix"
g
.. gendata
SELECT *
FROM (
SELECT DISTINCT ON (t.payroll, ?."TrainingName", ?."Institute")
g."Employee Name", g."Position", g."Department", t.*
FROM "TrainingMatrix" t
JOIN gendata g ON g.payroll = t.payroll
ORDER BY t.payroll, ?."TrainingName", ?."Institute"
, ?."TrainingDate" DESC NULLS LAST
) AS foo
WHERE foo."ExpiryDate" - current_date <= 0
AND EXTRACT(YEAR FROM foo."ExpiryDate") = EXTRACT(YEAR FROM current_date);
但还有更多。
像@a_horse写的那样,使用必须一直双引号的非法标识符是个坏主意。但是,带有封闭空格字符的标识符更糟糕:"Employee Name"
。这距离自制的SQL注入还有一步之遥。
您的其他过滤器的措辞对性能不利。
WHERE "ExpiryDate" - current_date <= 0
不是sargable因此不能使用普通索引。抛开这一点,它也比它需要的更昂贵。改为使用:
WHERE "ExpiryDate" >= current_date
类似于你的第二个表达式,应该重写为:
WHERE "ExpiryDate" >= date_trunc('year', current_date)
AND "ExpiryDate" < date_trunc('year', current_date) + interval '1 year'
结合两者,我们可以删除冗余表达式:
WHERE "ExpiryDate" >= current_date
AND "ExpiryDate" < date_trunc('year', current_date) + interval '1 year'
您的问题含糊不清。是否要在DISTINCT
之前或之后应用其他过滤器?结果不同。
假设在 DISTINCT
之前的,您不需要子查询 - 这会消除您的直接问题的原因:子查询没有别的别名。
所有在一起:
SELECT DISTINCT ON (t.payroll, "TrainingName", "Institute")
g."Employee Name", g."Position", g."Department", t.*
FROM "TrainingMatrix" t
JOIN gendata g USING (payroll)
WHERE t."ExpiryDate" >= current_date
AND t."ExpiryDate" < date_trunc('year', current_date) + interval '1 year'
ORDER BY t.payroll, "TrainingName", "Institute", "TrainingDate" DESC NULLS LAST
答案 1 :(得分:6)
当您将实际查询包装到派生表(select .. from (...) as foo
)中时,您的“表格”不再被称为TrainingMatrix
。您需要使用用于派生表的别名来引用它:
select *
from (
... you original query ..
) as foo
where foo."ExpiryDate" - current_date <= 0
and extract(year from foo."ExpiryDate") = extract(year from current_date)
顺便说一句:我建议你停止使用引用标识符"ExpiryDate"
,使用区分大小写的名称通常会给你带来更多的麻烦。