WHERE中不允许使用聚合函数

时间:2017-01-31 09:46:07

标签: sql postgresql

我使用此查询使用postgresql按最新日期查找唯一记录。我遇到的错误是“在WHERE中不允许聚合函数”。 How to fix error “aggregate functions are not allowed in WHERE”根据此链接,我尝试使用内部选择功能。但这没效果。请帮我编辑查询。我使用PgAdmin III作为客户端。

SELECT Distinct t1.pa_serial_
    ,t1.homeownerm_name
    ,t1.districtvdc
    ,t1.date as firstrancheinspection_date
    ,t1.status
    ,t1.name_of_data_collector
    ,t1.fulcrum_id
    ,first_tranche_inspection_v2_reporting_questionnaire.date_reporting
From first_tranche_inspection_v2 t1
LEFT JOIN first_tranche_inspection_v2_reporting_questionnaire ON (t1.fulcrum_id = first_tranche_inspection_v2_reporting_questionnaire.fulcrum_parent_id)
where first_tranche_inspection_v2_reporting_questionnaire.date_reporting = (
        select Max(first_tranche_inspection_v2_reporting_questionnaire.date_reporting)
        from first_tranche_inspection_v2
        where first_tranche_inspection_v2.pa_serial_ = t1.pa_serial_
        );

2 个答案:

答案 0 :(得分:2)

您希望每次检查都加入最新的报告问卷。在PostgreSQL中,您可以使用DISTINCT ON

select fti.*, rq.* 
from first_tranche_inspection_v2 fti
left join
(
  select distinct on (fulcrum_parent_id) *
  from first_tranche_inspection_v2_reporting_questionnaire
  order by fulcrum_parent_id, date_reporting desc
) rq on rq.fulcrum_parent_id = fti.fulcrum_id;

或使用标准SQL的ROW_NUMBER

select fti.*, rq.* 
from first_tranche_inspection_v2 fti
left join
(
  select 
    ftirq.*, 
    row_number() over (partition by fulcrum_parent_id order by date_reporting desc) as rn
  from first_tranche_inspection_v2_reporting_questionnaire ftirq
) rq on rq.fulcrum_parent_id = fti.fulcrum_id and rq.rn = 1;

你要做的事情应该是这样的:

select fti.*, rq.* 
from first_tranche_inspection_v2 fti
left join first_tranche_inspection_v2_reporting_questionnaire rq
  on rq.fulcrum_parent_id = fti.fulcrum_id
  and (rq.fulcrum_parent_id, rq.date_reporting) in
  (
    select fulcrum_parent_id, max(date_reporting)
    from first_tranche_inspection_v2_reporting_questionnaire
    group by fulcrum_parent_id
  );

这也有效,并且只有两次阅读表first_tranche_inspection_v2_reporting_questionnaire的缺点。

答案 1 :(得分:1)

DISTINCT通常最终会在许多RDBMS中使用GROUP BY查询实现。我认为您当前查询中发生的事情是,已经存在涉及SELECT中的列的隐式聚合。因此,由于MAX(),涉及DISTINCT的相关子查询实际上是一个聚合。

一个快速解决方法可能是在没有DISTINCT的情况下执行原始查询,然后子查询结果集以仅保留不同的记录:

WITH cte AS (
    SELECT t1.pa_serial_,
           t1.homeownerm_name,
           t1.districtvdc,
           t1.date as firstrancheinspection_date,
           t1.status,
           t1.name_of_data_collector,
           t1.fulcrum_id,
           t2.date_reporting
    FROM first_tranche_inspection_v2 t1
    LEFT JOIN first_tranche_inspection_v2_reporting_questionnaire t2
        ON t1.fulcrum_id = t2.fulcrum_parent_id
    WHERE t2.date_reporting = (SELECT MAX(t.date_reporting)
                               FROM first_tranche_inspection_v2 t
                               WHERE t.pa_serial_ = t1.pa_serial_)
);

SELECT DISTINCT t.pa_serial_,
                t.homeownerm_name,
                t.districtvdc,
                t.firstrancheinspection_date,
                t.status,
                t.name_of_data_collector,
                t.fulcrum_id,
                t.date_reporting
FROM cte t

请注意,我继续为您的联接中的第二个表添加别名,这使查询更容易阅读。