Return only rows whose max value is less than specified

五迷三道 提交于 2019-12-06 01:42:49

You want to pull the condition from the join into a having clause. The join is only looking at records before those dates, so you don't know if anything happens afterwards.

SELECT *
FROM tbldealermobiles
  INNER JOIN tblhistory ON tbldealermobiles.FCS = tblhistory.FCS
  INNER JOIN tblAllDealers ON tbldealermobiles.FCS = tblAllDealers.FCS
WHERE tblAllDealers.CustGroup in ('Virtual', 'Outbound')
GROUP BY tbldealermobiles.mobilenumber 
HAVING MAX(tblhistory.PurchaseDate) <
            MAX(case when tblAllDealers.CustGroup = 'Virtual' then date('2013-03-22')
                     when tblAllDealers.CustGroup = 'Outbound' then date('2013-04-21')
                end)
ORDER BY tblhistory.PurchaseDate DESC
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!