1

嗨,我在这里问了一个问题:仅返回最大值小于指定的行

我得到的答案非常适合我想要的:

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

提前致谢。

4

2 回答 2

2

我会像这样完成你的原始查询..

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

然后,对于这个查询,只需将<符号更改为>.

于 2013-05-22T14:47:10.327 回答
0

@ch3my- 我可能在这里遗漏了一些东西,但是将AND PurchaseDate >= '2013-05-01'添加到您的 WHERE 子句中(将日期设置为您要查找的最短日期)怎么样

于 2013-05-22T14:19:05.127 回答