将聚合函数添加到WHERE子句中?

Pad*_*mez 3 sql postgresql aggregate-functions where

我想做一份报告,告诉所有在过去75天左右没有被呼叫过的客户.我的专栏如下.

Customer# Customer_Name Phone_Number Call_Date Salesman

通话日期会提取客户被叫方的日期.

这是我当前的查询.

select customer_no
      ,Customer_name
      ,Phone_number
      ,max(Call_Date) as Call_date
      ,Salesman
from salescalls
where call_date <= current_date - 75
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是它拉动每个客户并使用他们最后一次调用75天或更多天.

例如,当最后一个通话日期是6/4/14时,它会提取数字,并将通话日期列为11/10/13.

它不应该列出在过去75天内被调用的客户.所以为了防止这种情况,我试图在where子句中这样做.

Where max(call_date) <= current_date - 75
Run Code Online (Sandbox Code Playgroud)

但这只是给我一个错误:

aggregates not allowed in WHERE clause
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 15

你想要一个having条款:

select customer_no, Customer_name, Phone_number, max(Call_Date) as Call_date,
       Salesman
from salescalls
group by customer_no, Customer_name, Phone_number, Salesman
having max(call_date) <= current_date - 75;
Run Code Online (Sandbox Code Playgroud)

您不能将聚合函数放在where子句中.


Isw*_*San 5

你需要把你的条件放在HAVING子句中.

having max(call_date) <= current_date - 75
Run Code Online (Sandbox Code Playgroud)