gpt4 book ai didi

mysql - 连接两个表未按预期工作

转载 作者:行者123 更新时间:2023-11-30 21:38:21 25 4
gpt4 key购买 nike

我已经为此工作了大约两天,但似乎无法弄明白,所以我希望得到一些帮助。

我有两个表:

mysql> select id, date, volume, symbol_id  from control_quotedaily limit 5;
+-------+---- -------+----------+-----------+
| id | date | volume | symbol_id |
+-------+------------+----------+-----------+
| 13263 | 2017-11-02 | 7800191 | AXISBANK |
| 13264 | 2017-11-02 | 9303981 | SBIN |
| 13265 | 2017-11-02 | 8013536 | HDFCBANK |
| 13266 | 2017-11-03 | 9642624 | AXISBANK |
| 13267 | 2017-11-04 | 19642327 | AXISBANK |
+-------+------------+----------+-----------+
5 rows in set (0.00 sec)
14 rows in set (0.01 sec)

mysql> select * from control_oidaily
+-----------+------------+-------------+--------------+
| symbol_id | date | expiry_date | val_in_lakhs |
+-----------+------------+-------------+--------------+
| AXISBANK | 2017-11-02 | 2017-11-30 | 166881.8 |
| AXISBANK | 2017-11-02 | 2017-12-28 | 2676.84 |
| AXISBANK | 2017-11-02 | 2018-01-25 | 97.13 |
| HDFCBANK | 2017-11-02 | 2017-11-30 | 76351.11 |
| HDFCBANK | 2017-11-02 | 2017-12-28 | 1509.48 |
| HDFCBANK | 2017-11-02 | 2018-01-25 | 0 |
| SBIN | 2017-11-02 | 2017-11-30 | 88654.3 |
| SBIN | 2017-11-02 | 2017-12-28 | 1060.51 |
| SBIN | 2017-11-02 | 2018-01-25 | 0 |
| AXISBANK | 2017-11-03 | 2017-11-30 | 87640.06 |
+-----------+------------+-------------+--------------+

所以对于 control_quotedai​​ly 表中的每个引用,这就是我想要的:

  1. control_oidaily 表中该报价的最接近 expiry_date
  2. 我想要那个 expiry_date 的 val_in_lakhs。

例如:对于日期 2017-11-02,最接近的到期日是 2017-11-30,我希望返回 val_in_lakhs (76351.11)。

这就是我正在尝试的:

select o.date, o.expiry_date as expiry_date, o.symbol_id, q.date, q.symbol_id, o.val_in_lakhs, q.tottrdval, q.volume, q.symbol_id
FROM control_oidaily o
JOIN ( select o.date, MIN(expiry_date) as expiry_date, symbol_id
FROM control_oidaily o
GROUP by o.date,o.symbol_id
ORDER BY o.date asc) as ed
ON ed.date = o.date
AND ed.symbol_id = o.symbol_id
AND ed.expiry_date = o.expiry_date
JOIN control_quotedaily q
ON q.date = ed.date
AND q.symbol_id = ed.symbol_id

这是我期待的输出:

+-------+------------+----------+-----------+--------------+--------------+
| id | date | volume | symbol_id | expiry_date | val_in_lakhs |
+-------+------------+----------+-----------+--------------+--------------+
| 13263 | 2017-11-02 | 7800191 | AXISBANK | 2017-11-30 | 166881.8 |
| 13264 | 2017-11-02 | 9303981 | SBIN | 2017-11-30 | 88654.3 |
| 13265 | 2017-11-02 | 8013536 | HDFCBANK | 2017-11-30 | 76351.11 |
| 13266 | 2017-11-03 | 9642624 | AXISBANK | 2017-11-30 | 87640.06 |
+-------+------------+----------+-----------+--------------+--------------+

最佳答案

  • Derived Table , 得到 closest_expiry_date (expiry_date 的最小值),对于 symbol_id 的分组和 date , 来自 control_oidaily表。
  • 加入 control_quotedailysymbol_id 上的 table 和 date , 获取 control_quotedaily 中每一行的 closest_expiry_date 值表。
  • 现在,再次使用相同的派生表与 control_oidaily 连接表,匹配 date , expiry_date (最近到期日)和 symbol_id得到val_in_lakhs .

您可能需要以下查询:

SELECT q.id, 
q.`date`,
q.volume,
q.symbol_id,
o1.expiry_date,
o1.val_in_lakhs
FROM control_quotedaily AS q
JOIN (SELECT o2.`date`,
o2.symbol_id
MIN(o2.expiry_date) as closest_expiry_date
FROM control_oidaily AS o2
GROUP by o2.`date`, o2.symbol_id
) AS dt
ON dt.`date` = q.`date`
AND dt.symbol_id = q.symbol_id
JOIN control_oidaily AS o1
ON dt.`date` = o1.`date`
AND dt.symbol_id = o1.symbol_id
AND dt.closest_expiry_date = o1.expiry_date

关于mysql - 连接两个表未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52912567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com