0
我有一個表看起來像這樣:MySQL的:時間戳行之間轉移數據
mysql> explain test_table;
+---------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+---------+-------+
| timestamp | datetime | NO | PRI | NULL | |
| id | varchar(64) | NO | PRI | NULL | |
| px_last | float | NO | | NULL | |
| twap | float | YES | | NULL | |
+---------------------+-------------+------+-----+---------+-------+
對於給定的符號和時間戳,我要到外地twap
的值一個時步移回。
該表:
mysql> select * from test_table;
+---------------------+-------+---------+-------+
| timestamp | id | px_last | twap |
+---------------------+-------+---------+-------+
| 2011-01-01 00:00:00 | apple | 101 | 101.1 |
| 2011-01-01 00:00:00 | pear | 50 | 50.1 |
| 2011-01-02 00:00:00 | apple | 102 | 112.8 |
| 2011-01-02 00:00:00 | pear | 51 | 57.3 |
| 2011-01-03 00:00:00 | pear | 52 | 59.1 |
| 2011-01-03 00:00:00 | apple | 103 | 104.1 |
+---------------------+-------+---------+-------+
應該結束了這樣的:
mysql> select * from test_table;
+---------------------+-------+---------+-------+
| timestamp | id | px_last | twap |
+---------------------+-------+---------+-------+
| 2011-01-01 00:00:00 | apple | 101 | 112.8 |
| 2011-01-01 00:00:00 | pear | 50 | 57.3 |
| 2011-01-02 00:00:00 | apple | 102 | 104.1 |
| 2011-01-02 00:00:00 | pear | 51 | 59.1 |
| 2011-01-03 00:00:00 | pear | 52 | NULL |
| 2011-01-03 00:00:00 | apple | 103 | NULL |
+---------------------+-------+---------+-------+
我的第一種方法(使用本例中:How to number rows...)是創建一個行號(1,2 .. ..n),然後將行號,符號和twap複製到臨時表中,在臨時表的每一行中將行號減1,然後將數據複製回原始表。
有沒有辦法做到這一點,而不創建臨時表?這個表格非常龐大,超過5000萬行並且不斷增長,因此找到比給定時間戳更小的最大時間戳記效率不夠高。
感謝您的更正和建議!但是,間隔有時會超過一天(假期等),在這種情況下,我希望您的解決方案發生故障,對吧? – mxns
@MånsRolandiDanielsson:是的,這個解決方案要求連續幾天。 – ruakh