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

标签 mysql date select join

我有 2 个表,我想在其中计算值。第一个表提供历史价格数据(价格),第二个表包含年度数据(数量),其中包含历史数据和预测数据。到目前为止,我可以连接这两个表,以便我可以通过每日表中相应日期的价格来计算历史年度值,但不能计算 future 年度值,我想根据每日表中的最后一天价格来计算价格表。

当前表结构

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

当前查询

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;

结果采用 2012-2013 年数据而不是 2012-2016 年数据:

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

我的年度表(成交量)包含 2014 年、2015 年和 2016 年的成交量估算(未显示),并且每日表(价格)中的最后价格为 65,但上述查询仅将成交量与每日价格表中相应的日期值,而不是使用最近的最后值。有人可以建议我如何最好地做到这一点吗?

表示例(注意:两个表都使用“日期”和“代码”周围的复合主键)

数量

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

价格

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

最佳答案

如果您希望查询通过将 future 预计单位数量乘以记录的最新价格来获取 future 值,那么这就是查询。查看工作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
<小时/>

根据OP请求更新了查询..工作FIDDLE 这个计算与日期相关的价格。一旦您到达 future 日期,它就会用 future 的单位计算最后的价格

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

关于MySQL根据另一个表中最近日期的价格计算值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22792784/

相关文章:

php - 选择并获取 mysql 上重复次数最多的事件

Mysql查询根据条件选择不同的记录?

php - 制作PHP照片库的有效方法

php - 遍历日期范围内的所有月份?

ssl - OCaml-ssl with Unix.select 导致读取错误

sql - 如何找出 postgresql 中两个参数之间的值?

php - laravel 5 将日期和时间插入数据库

MySQL让玩家在日期之间排名

mysql - MySQL中如何确定一组的第一个结果?

mysql - rails 4 : Assign the records of one relation to another