连接两个表未按预期工作

jecbmhm3  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(427)

我已经在这工作了大约两天了,现在似乎无法解决它,所以我想得到一些帮助。
我有两张table:

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-quoteday表中的每个引号,这就是我想要的:
控制表中该报价的最近到期日
我要那到期日的价值10万英镑。
例如:日期2017-11-02,最接近的到期日是2017-11-30,我要退10万欧元(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 |
+-------+------------+----------+-----------+--------------+--------------+
bf1o4zei

bf1o4zei1#

在派生表中,获取 closest_expiry_date (最小值) expiry_date ),对于 symbol_id 以及 date ,来自 control_oidaily table。
把这个和 control_quotedaily 上的表 symbol_id 以及 date ,以获取 control_quotedaily table。
现在,再次使用相同的派生表与 control_oidaily table,配对 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

相关问题