我已经为此工作了大约两天,似乎无法弄清楚,因此我希望获得一些帮助。

我有两个表:

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表中的每个报价,这就是我想要的:


control_oidaily表中与该报价最接近的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),将datecontrol_oidaily分组。
将其与control_quotedailysymbol_id上的date表连接起来,以获取control_quotedaily表中每一行的closet_expiry_date值。
现在,再次使用相同的派生表与control_oidaily表联接,在dateexpiry_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/

10-12 06:11