[英]Get max(date) from table
Hopefully this is a simple one. 希望这是一个简单的例子。 I have a table of sent messages and a customer table. 我有一个已发送消息表和一个客户表。 I am trying to get the date of the last message sent to a customer, but I am having a nightmare with it and can't work out what's up!! 我试图获取发送给客户的最后一条消息的日期,但是我对此感到恶梦,无法解决问题!!
In english: Get me the last date the customer received where the last date is greater than DateX and less than DateY. 用英语:为我获取客户收到的最后日期,该日期的最后一个日期大于DateX且小于DateY。
In my SQL: (Msaccess) 在我的SQL中:(Msaccess)
SELECT
Max(outgoingmessages.outgoingmessagedatetime),
outgoingmessages.outgoingmessagecustomerID
FROM
outgoingmessages
inner join customers on customers.customerid =
outgoingmessages.outgoingmessagecustomerid
WHERE
(outgoingmessages.outgoingmessagedatetime>#20/Oct/2012#
and
outgoingmessages.outgoingmessagedatetime < #02/Nov/2012# )
and
outgoingmessages.outgoingmessagecustomerID NOT IN (
SELECT incomingMessageCustomerID from incomingmessages
)
GROUP BY
outgoingmessages.outgoingmessagecustomerID;
I know that customerid=32
has had a message sent on 05/11, yet the query seems to be ignoring this and showing an old date of his. 我知道customerid=32
在05/11上已发送了一条消息,但查询似乎忽略了它并显示了他的旧日期。 I literally just need to get the customerID
and the date of their last message. 我实际上只需要获取customerID
和他们最后一条消息的日期。
any ideas?! 有任何想法吗?!
================ UPDATE ================更新
I think the statement would work if I could say "WHERE MAX(outgoingmessagetime) BETWEEN date1 and date2)... but I cannot use the MAX date. Maybe I need a sub query? 我认为如果我可以说“ date1和date2之间的最大MAX(outgoingmessagetime)...”但我不能使用MAX日期,则该语句将起作用。也许我需要一个子查询?
Why not ... ? 为什么不 ... ?
SELECT Top 1 o.outgoingmessagedatetime, o.outgoingmessagecustomerID
FROM outgoingmessages o
WHERE o.outgoingmessagedatetime between #2012/10/02# And #2012/11/02#
ORDER BY o.outgoingmessagedatetime Desc
Note that this will not include messages sent after 2nd November. 请注意,这不包括11月2日之后发送的消息。 To include messages up to 5th November, say 例如,要包含截至11月5日的邮件
WHERE o.outgoingmessagedatetime between #2012/10/02# And #2012/11/05#
Re Comment 重新评论
SELECT o.outgoingmessagecustomerID, Max(o.outgoingmessagedatetime)
FROM outgoingmessages o
WHERE o.outgoingmessagedatetime between #2012/10/02# And #2012/11/02#
AND o.outgoingmessagecustomerID NOT IN (
SELECT outgoingmessagecustomerID FROM outgoingmessages
WHERE outgoingmessagedatetime > #2012/11/02#)
GROUP BY o.outgoingmessagecustomerID
How about this, at first I didn't think the customers table was required but it looks as though you may only want to show records that match customers in that table so I've gone for this approach: 怎么样,起初我不认为有必要使用customers表,但是看起来您可能只想在该表中显示与客户匹配的记录,因此我采用了这种方法:
SELECT A.MaxDT, A.outgoingmessagecustomerID
FROM
(SELECT Max(O.outgoingmessagedatetime) AS MaxDT, O.outgoingmessagecustomerID
FROM outgoingmessages AS O
WHERE O.outgoingmessagedatetime BETWEEN >#10/21/2012# AND Date()-7
AND O.outgoingmessagecustomerID NOT IN (SELECT incomingMessageCustomerID FROM imcomingmessages)
GROUP BY O.outgoingmessagecustomerID) AS A, customers AS C
WHERE A.outgoingmessagecustomerID = C.customerID
This carries out the MAX calculation in a derived table and then just links the results back against the customer table 这将在派生表中执行MAX计算,然后将结果链接回客户表
Ps: I wish your field names were shorter ^_^ 附言:我希望您的字段名称较短^ _ ^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.