您好我在这里问了一个问题:Return only rows whose max value is less than specified
我得到的答案非常适合我的想法:
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
我试图通过简单地将小于号码更改为大于这样的符号来重新定位它:
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
但我得到的移动号码在指定的日期之前,这个查询可以像这样使用,还是需要重新编写?
输出:
我得到795行,这是最后15行:
PurchaseDate CustGroup
2012-05-12 Outbound
2012-05-12 Outbound
2012-05-11 Virtual
2012-05-10 Virtual
2012-05-10 Outbound
2012-05-10 Outbound
2012-05-10 Virtual
2012-05-10 Outbound
2012-05-10 Outbound
2012-05-10 Virtual
2012-05-09 Outbound
2012-05-09 Outbound
2012-05-09 Outbound
2012-05-08 Outbound
2012-05-08 Outbound
提前致谢。
答案 0 :(得分:2)
我会像这样完成您的ORIGINAL查询..
select *
FROM tbldealermobiles m
INNER JOIN tblhistory h ON m.FCS = h.FCS
INNER JOIN tblAllDealers a ON m.FCS = a.FCS
WHERE a.CustGroup in ('Virtual', 'Outbound')
and h.PurchaseDate = (
select max(h2.PurchaseDate) from tblhistory h2
where h2.fcs = a.fcs)
and (a.CustGroup = 'Virtual' AND
h.PurchaseDate < date('2013-03-22')
or
a.CustGroup = 'Outbound' AND
h.PurchaseDate < date('2013-04-21'))
ORDER BY h.PurchaseDate DESC
然后,对于此查询,只需将<
符号更改为>
。
答案 1 :(得分:0)
@ ch3my-我可能在这里遗漏了一些东西,但是如何将 AND PurchaseDate&gt; ='2013-05-01'添加到WHERE子句中(将日期设置为您的最短日期寻找)