select * from tablename where 1<=datediff(month,max(duedate),getdate())<=1 group by emp_id

解决方案 »

  1.   

    这句错了.服务器: 消息 147,级别 15,状态 1,行 3
    聚合不应出现在 WHERE 子句中,除非该聚合位于 HAVING 子句或选择列表所包含的子查询中,并且要对其进行聚合的列是外部引用。
      

  2.   

    select a.emp_id from emp_contract as a,(select emp_id,max(duedate) as duedate from emp_contract group by emp_id) as b where a.emp_id=b.emp_id and datediff(month,getdate(),b.duedate)<=1 and datediff(month,getdate(),b.duedate)>0