0

我正在尝试获取满足此查询的 WHERE 条件的所有行,并按tickerSymbol 分组。每个tickerSymbol 有多行,所以我只是想为每个tickerSymbol 获取最后一行,这就是我按时间戳排序的原因。

这是表格:

+----+--------------+---------+---------+---------+---------------------+
| id | tickerSymbol | session | quote   | volume  | timestamp           |
+----+--------------+---------+---------+---------+---------------------+
|  6 | ADS          |       2 |  226.81 |    2698 | 2017-10-06 09:38:36 |
| 15 | AFI          |       2 |   16.09 |    5565 | 2017-10-06 09:38:52 |
| 96 | CNSL         |       2 |   20.06 |    8396 | 2017-10-06 09:39:11 |
| 81 | DELT         |       2 |    1.43 |  134867 | 2017-10-06 09:39:15 |
| 75 | FLXN         |       2 |    27.7 |  125331 | 2017-10-06 09:38:58 |
| 27 | HXL          |       2 |   58.69 |    3106 | 2017-10-06 09:39:02 |
| 45 | MMP          |       2 | 70.5418 |    8069 | 2017-10-06 09:38:30 |
| 51 | NBIX         |       2 |   62.43 |   21064 | 2017-10-06 09:39:15 |
| 39 | NFLX         |       2 | 193.269 | 1394992 | 2017-10-06 09:39:16 |
| 42 | NLST         |       2 |  0.7199 |   20011 | 2017-10-06 09:39:13 |
| 12 | NLST         |       2 |    0.72 |   10010 | 2017-10-06 09:38:50 |
| 33 | NTES         |       2 | 273.217 |   33064 | 2017-10-06 09:39:12 |
| 57 | NWS          |       2 | 13.6677 |    8958 | 2017-10-06 09:39:14 |
| 21 | REGI         |       2 | 12.0603 |   10068 | 2017-10-06 09:39:02 |
| 99 | RGEN         |       2 |   38.23 |   14478 | 2017-10-06 09:39:02 |
| 93 | RNVA         |       2 |  2.5105 |   40474 | 2017-10-06 09:38:53 |
| 72 | S            |       2 |    7.51 |  833302 | 2017-10-06 09:39:13 |
| 84 | SBNY         |       2 |   126.1 |   53815 | 2017-10-06 09:39:13 |
| 24 | SNDR         |       2 |   24.82 |    1237 | 2017-10-06 09:39:04 |
| 66 | SO           |       2 |   49.08 |  128862 | 2017-10-06 09:39:16 |
| 78 | SQ           |       2 | 30.6947 |  342939 | 2017-10-06 09:39:15 |
|  9 | SWM          |       2 | 41.2975 |    3071 | 2017-10-06 09:38:50 |
| 69 | SWN          |       2 |    6.02 |  844406 | 2017-10-06 09:39:15 |
| 30 | TGNA         |       2 |   13.49 |   23129 | 2017-10-06 09:39:13 |
| 36 | THC          |       2 |   15.91 |   84785 | 2017-10-06 09:39:14 |
| 18 | TIER         |       2 |   19.52 |    5234 | 2017-10-06 09:38:53 |
| 87 | UGLD         |       2 |    10.3 |   91900 | 2017-10-06 09:39:13 |
| 90 | USB          |       2 |   54.23 |  201985 | 2017-10-06 09:39:14 |
| 63 | REGI         |       2 |   12.24 |   21527 | 2017-10-06 09:40:13 |
| 48 | RGEN         |       2 |   38.33 |   28344 | 2017-10-06 09:40:12 |
| 54 | RNVA         |       2 |    2.63 |   67512 | 2017-10-06 09:40:14 |
+----+--------------+---------+---------+---------+---------------------+

我正在使用以下查询。该查询的工作原理是它返回tickerSymbol 为REGI、RGEN 或RNVA 的3 行,但报价列数据错误?

SELECT id, tickerSymbol, quote, MAX(timestamp) AS timestamp 
FROM `dataPoints__quotes` 
WHERE `tickerSymbol`='REGI' or `tickerSymbol`='RGEN' or `tickerSymbol`='RNVA'  
GROUP BY `tickerSymbol` 
ORDER BY `timestamp` DESC;

当我运行查询时,我得到:

+----+--------------+---------+---------------------+
| id | tickerSymbol | quote   | timestamp           |
+----+--------------+---------+---------------------+
| 93 | RNVA         |  2.5105 | 2017-10-06 09:40:14 |
| 21 | REGI         | 12.0603 | 2017-10-06 09:40:13 |
| 99 | RGEN         |   38.23 | 2017-10-06 09:40:12 |
+----+--------------+---------+---------------------+

就像我想要的那样,返回了 3 行,但是 id 和 quote 列中的数据是错误的。

SQLFiddle

这是我在 SQLFiddle 上运行的表 + 数据,http: //sqlfiddle.com/#!9/7aa8f9/2/0

CREATE TABLE IF NOT EXISTS `dataPoints__quotes` 
(`id` int  NOT NULL PRIMARY KEY
,`tickerSymbol` varchar(6) NOT NULL
,`session` int(11) NOT NULL
,`quote` float NOT NULL
,`volume` int(20) NOT NULL
,`timestamp` datetime NOT NULL
, INDEX(tickerSymbol)
);

INSERT INTO `dataPoints__quotes` (`id`, `tickerSymbol`, `session`, `quote`, `volume`, `timestamp`) VALUES
(6, 'ADS', 2, 226.81, 2698, '2017-10-06 09:38:36.180000'),
(15, 'AFI', 2, 16.09, 5565, '2017-10-06 09:38:52.054000'),
(96, 'CNSL', 2, 20.06, 8396, '2017-10-06 09:39:11.407000'),
(81, 'DELT', 2, 1.43, 134867, '2017-10-06 09:39:15.769000'),
(75, 'FLXN', 2, 27.7, 125331, '2017-10-06 09:38:58.666000'),
(27, 'HXL', 2, 58.69, 3106, '2017-10-06 09:39:02.898000'),
(45, 'MMP', 2, 70.5418, 8069, '2017-10-06 09:38:30.283000'),
(51, 'NBIX', 2, 62.43, 21064, '2017-10-06 09:39:15.688000'),
(39, 'NFLX', 2, 193.269, 1394992, '2017-10-06 09:39:16.410000'),
(42, 'NLST', 2, 0.7199, 20011, '2017-10-06 09:39:13.298000'),
(12, 'NLST', 2, 0.72, 10010, '2017-10-06 09:38:50.789000'),
(33, 'NTES', 2, 273.217, 33064, '2017-10-06 09:39:12.649000'),
(57, 'NWS', 2, 13.6677, 8958, '2017-10-06 09:39:14.187000'),
(21, 'REGI', 2, 12.0603, 10068, '2017-10-06 09:39:02.331000'),
(99, 'RGEN', 2, 38.23, 14478, '2017-10-06 09:39:02.986000'),
(93, 'RNVA', 2, 2.5105, 40474, '2017-10-06 09:38:53.685000'),
(72, 'S', 2, 7.51, 833302, '2017-10-06 09:39:13.726000'),
(84, 'SBNY', 2, 126.1, 53815, '2017-10-06 09:39:13.033000'),
(24, 'SNDR', 2, 24.82, 1237, '2017-10-06 09:39:04.891000'),
(66, 'SO', 2, 49.08, 128862, '2017-10-06 09:39:16.498000'),
(78, 'SQ', 2, 30.6947, 342939, '2017-10-06 09:39:15.179000'),
(9, 'SWM', 2, 41.2975, 3071, '2017-10-06 09:38:50.675000'),
(69, 'SWN', 2, 6.02, 844406, '2017-10-06 09:39:15.382000'),
(30, 'TGNA', 2, 13.49, 23129, '2017-10-06 09:39:13.274000'),
(36, 'THC', 2, 15.91, 84785, '2017-10-06 09:39:14.359000'),
(18, 'TIER', 2, 19.52, 5234, '2017-10-06 09:38:53.558000'),
(87, 'UGLD', 2, 10.3, 91900, '2017-10-06 09:39:13.731000'),
(90, 'USB', 2, 54.23, 201985, '2017-10-06 09:39:14.986000'),
(63, 'REGI', 2, 12.24, 21527, '2017-10-06 09:40:13.093000'),
(48, 'RGEN', 2, 38.33, 28344, '2017-10-06 09:40:12.472000'),
(54, 'RNVA', 2, 2.63, 67512, '2017-10-06 09:40:14.703000');

你会看到它返回:

id  tickerSymbol    quote   timestamp
93  RNVA    2.5105  2017-10-06T09:40:14.703Z
21  REGI    12.0603 2017-10-06T09:40:13.093Z
99  RGEN    38.23   2017-10-06T09:40:12.472Z

查询在 id + quote 列中返回错误的数据。它应该返回:

id  tickerSymbol    quote   timestamp
54  RNVA    2.63    2017-10-06T09:40:14.703Z
63  REGI    12.24   2017-10-06T09:40:13.093Z
48  RGEN    38.33   2017-10-06T09:40:12.472Z
4

1 回答 1

1
SELECT x.* 
  FROM datapoints__quotes x
  JOIN 
     ( SELECT tickersymbol
            , MAX(timestamp) timestamp  
         FROM datapoints__quotes 
        WHERE tickersymbol IN('REGI','RGEN','RNVA') 
        GROUP 
           BY tickersymbol
     ) y
    ON y.tickersymbol = x.tickersymbol 
   AND y.timestamp = x.timestamp;
+----+--------------+---------+-------+--------+---------------------+
| id | tickerSymbol | session | quote | volume | timestamp           |
+----+--------------+---------+-------+--------+---------------------+
| 63 | REGI         |       2 | 12.24 |  21527 | 2017-10-06 09:40:13 |
| 48 | RGEN         |       2 | 38.33 |  28344 | 2017-10-06 09:40:12 |
| 54 | RNVA         |       2 |  2.63 |  67512 | 2017-10-06 09:40:14 |
+----+--------------+---------+-------+--------+---------------------+

将您的股票代码索引更改为 (tickersymbol,timestamp)

于 2017-10-06T14:44:29.343 回答