简体   繁体   English

MySQL在另一个表中根据最近日期的价格计算值

[英]MySQL to calculate a value against price at the nearest date in another table

I have 2 tables where I want to calculate a value across. 我有2个表格,我想在其中计算一个值。 One table provides daily price data (prices) which is historic while the second contains annual data (volumes) which contains both historic and projected data. 一个表提供历史价格的每日价格数据(价格),第二个表包含历史数据和预测数据的年度数据(数量)。 So far, I can join the two tables so that I can calculate the historic annual value by the price at the corresponding date in the daily one, but not for the future annual values where I would like to calculate against the last daily price in the prices table. 到目前为止,我可以将这两个表结合起来,以便可以通过每日中相应日期的价格来计算历史年度价值,但是不能针对将来要根据价格中的最近每日价格进行计算的未来年度价值进行计算价格表。

Current table structure 当前表结构

Table 1: Prices   (3 fields: date,code,price)
Table 2: Volumes  (3 fields: date,code,units)

Current query 当前查询

SELECT v.date,p.price,v.units,CONCAT(p.price*v.units) AS Value
FROM Prices p,Volumes v
WHERE p.code = v.code AND p.date = v.date
AND v.code = 'X' AND (v.date BETWEEN '2012-12-31' AND '2016-12-31')
GROUP BY v.date
ORDER BY v.date
LIMIT 5;

results in 2012-2013 data rather than 2012-2016: 2012-2013年数据而非2012-2016年的结果:

date   2012-12-31   2013-12-31 
price    50            58
units   100            90
Value  5000           5220

My annual table (Volumes) has volume estimates for years 2014,2015 and 2016 (which are not shown) and the last price in the daily table (Prices) is say 65, but the above query will only match the volumes to the corresponding date value in the daily price table rather than using the nearest last value. 我的年表(体积)具有2014、2015和2016年的体积估计值(未显示),日表中的最后价格(价格)为65,但是上述查询只会将体积与相应日期匹配每日价格表中的值,而不是使用最近的最近值。 Can someone advise me on how best to do this please? 有人可以建议我如何最好地做到这一点吗?

example of tables (note: both tables use a composite primary key around 'date' and 'code') 表的示例(注意:两个表在“日期”和“代码”周围都使用复合主键)

Volumes 卷数

date           code    units
2012-12-31     X        100
2012-12-31     Y         50
2013-12-31     X         90
2013-12-31     Y         45
2014-12-31     X         95
2014-12-31     Y         47

Prices 价格

date           code    price
2013-12-31     X         50
2013-12-31     Y         25
2014-01-01     X         58
2014-01-01     Y         27
2014-01-02     X         59
2014-01-02     Y         30
-----
2014-03-31     X         48
2014-03-31     Y         26
2014-04-01     X         49
2014-04-01     Y         27
last data point

If you want a query to get the future values by multiplying the future projected unit quantity with the most recent price recorded then this would be the query. 如果您希望查询通过将将来的预计单位数量乘以记录的最新价格来获取将来的值,则该查询为。 see working FIDDLE 看到工作FIDDLE

SELECT 
  *, 
  (future_units*last_price) as projeted_value --- dont need the CONCAT for this calculation
FROM(
  SELECT 
    v.date, 
    v.units as future_units,
    (SELECT 
       price 
     FROM prices
     ORDER BY date desc --- desc for the most recent
     LIMIT 1) as last_price --- just need the first one
  FROM volumes v
  WHERE v.date > NOW() --- to make it a future date
)as t --- table needs alias

updated query per the OP request.. working FIDDLE This one calculates the price related with the date.. once you get to future dates it calculates the last price with the future units 根据OP请求更新的查询..有效字段这将计算与日期相关的价格..一旦到达将来的日期,它将计算带有将来单位的最后价格

SELECT
  *,
  if(DATE(the_date) > NOW(), --- if its a future date put in last_price, else put in the current_price
    (future_units*last_price),
    (future_units*current_price))AS value
FROM(
  SELECT
    v.date AS the_date,
    v.units AS future_units,
    v.code,
    p.price AS current_price,
    (SELECT
       price
     FROM prices
     ORDER BY date DESC
     LIMIT 1) AS last_price
  FROM prices p
  LEFT JOIN volumes v ON p.code = v.code
  WHERE p.date >= v.date OR p.date < v.date
  AND DATE(v.date) BETWEEN '2012-12-31' AND '2016-12-31' -- DATE() used to cut out timestamp for accurate comparison
  GROUP BY v.date, v.code, v.units
)AS t

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

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