简体   繁体   English

MySQL基于最大(时间戳)加入

[英]Mysql join based on max(timestamp)

I have a web app that lets a client define a daily rate for their service. 我有一个网络应用程序,可让客户定义其服务的每日费率。 For any given day, the client can enter two rates, an hourly rate (rateTypeId=1) and a dailyRate (rateTypeId=2). 对于任何给定的一天,客户端可以输入两个费率,一个小时费率(rateTypeId = 1)和一个DailyRate(rateTypeId = 2)。 These rates are often assigned in advance, and they often change. 这些费率通常是预先分配的,并且经常更改。 I need to track all assignments, but only pull the latest assigned rate. 我需要跟踪所有分配,但只提取最新分配的费率。

I have two tables. 我有两张桌子。 The first table simply defines my rate structure and looks like this (simplified): 第一张表只是定义了我的汇率结构,看起来像这样(简化):

Table: RateDefinitions 表:RateDefinitions

RATECODE ----- RATE
31 ---------------- 5.00
32 ---------------- 6.00
33 ---------------- 7.00

My second table tracks the rates assigned to given dates. 我的第二张表跟踪分配给给定日期的汇率。 More than one rate can be assigned to a given date, but we will only used the latest rate based on the 'entrytimestamp'. 给定日期可以分配多个汇率,但是我们只会基于“ entrytimestamp”使用最新汇率。

Table: Rates 表:费率

ID --- RATETYPEID --- RATECODE ------ DATE -------- ENTRYTIMESTAMP
1 ---------- 1 --------------- 31 ---------- 20091010 ---------- 1100000000
2 ---------- 2 --------------- 33 ---------- 20091010 ---------- 1100000000
3 ---------- 1 --------------- 32 ---------- 20091010 ---------- 1200000000

Now I'm having difficulty putting together a query that will pull all the latest rate assignments for a given timeframe. 现在,我很难将查询合并到给定时间范围内所有最新的费率分配。

I've tried: 我试过了:

select r.id, r.rateTypeId, r.rateCode, max(r.entryTimestamp), rd.rate
from rates r
join rateDefinitions rd
on r.rateCode=rd.rateCode
where date=20091010
group by startDate, rateTypeId

But that's not going to do it. 但这是行不通的。 I'm thinking I need a join on a subselect statement, but not sure. 我在想我需要在subselect语句上进行联接,但不确定。 My results should contain two rows per date, similar to: 我的结果每个日期应包含两行,类似于:

ID --- RATETYPEID --- RATECODE ---------- ENTRYTIMESTAMP ----- RATE
3 ----------- 1 --------------- 32 -------------------- 1200000000 ----------6.00
2 ----------- 2 --------------- 33 -------------------- 1100000000 ----------7.00

Thanks for any suggestions. 感谢您的任何建议。

This problem is fairly common. 这个问题相当普遍。 You need a subquery to throw out the max timestamp and the ratetypeid (which is the basis for grouping) and then select everything else from an inner join of this subquery rows and everything else. 您需要一个子查询来抛出最大时间戳和ratetypeid(这是分组的基础),然后从此子查询行的内部联接中选择所有其他内容。

For MySQL: 对于MySQL:

SELECT ratecode, rate, id, ratetypeid, date, entrytimestamp 

FROM ratedefinitions, 
(SELECT ratetypeid, MAX(entrytimestamp) AS max_timestamp FROM Rates 
GROUP BY ratetypeid) AS inner_table

WHERE

inner_table.ratetypeid = ratetypeid
AND innertable.max_timestamp = timestamp

I recommend a sub-query: 我建议一个子查询:

select r.id, r.rateTypeId, r.rateCode, max(r.entryTimestamp), rd.rate 
from  
rateDefinitions rd,
rate r,
    (select 
        rateCode, max(entryTimestamp) maxEntryTimestamp
    from
        rate
    where date=20091010
    group by rateCode) latestRates

where r.rateCode=rd.rateCode  AND
    r.rateCode = latestRates.rateCode AND
    r.entryTimestamp = latestRates.maxEntryTimestamp
group by startDate, rateTypeId

There's a big assumption here on my part. 我有一个很大的假设。 That is that in the rate table, for a given date and ratecode, the entrytimestamps are unique. 也就是说,在费率表中,对于给定的日期和费率代码,entrytimestamps是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM