晚上好,
希望编写一个MySQL,将三天前组中的最新值(“关闭”)与同一组的最新值进行比较。这不是问题,它有效。但它没有按日期排序。每个新条目都有一个时间戳,正如我已经说过的那样,我想要一天的最新条目。
SELECT p1.ticker, p1.date, p1.close, p1.close - p2.close
FROM prices AS p1
LEFT JOIN prices AS p2 ON p1.ticker = p2.ticker
WHERE DATE( p2.DATE ) = CURDATE( ) -3
AND DATE( p1.DATE ) = CURDATE( )
GROUP BY p1.ticker
ORDER BY p1.DATE DESC , p2.Date DESC
你看我使用Curdate,但我也试过了Current_Timestamp。 p1.close - p2.close的结果显示SQL输入了错误的条目。错了=不是最新的。如果你能提供帮助,谢谢你。
---编辑---示例数据
CREATE TABLE prices (
ticker varchar(15) DEFAULT NULL,
`Date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`Close` decimal(24,4) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Data for table 'prices'
--
INSERT INTO prices (ticker, Date, Close) VALUES
('FB', '2014-04-01 16:24:29', 22.0000),
('FB', '2014-04-01 16:24:38', 23.0000),
('FB', '2014-04-04 15:09:46', 33.0000),
('FB', '2014-04-04 15:09:52', 36.0000),
('FB', '2014-04-04 20:09:42', 72.0000),
('FB', '2014-04-04 20:09:31', 61.0000),
('FB', '2014-04-04 20:09:19', 57.2500),
('FB', '2014-04-04 20:09:13', 57.0000),
('FB', '2014-04-04 20:09:06', 56.0000),
('FB', '2014-04-04 20:08:37', 52.2300),
('FB', '2014-04-04 20:08:25', 55.0000),
('FB', '2014-04-04 20:08:05', 65.0000),
('FB', '2014-04-04 20:07:54', 77.0000),
('FB', '2014-04-04 20:08:00', 71.0000),
('FB', '2014-04-04 20:09:48', 69.0000),
('FB', '2014-04-04 20:09:53', 66.0000),
('FB', '2014-04-04 20:09:59', 62.0000),
('FB', '2014-04-04 20:10:05', 63.0000),
('FB', '2014-04-04 20:10:15', 42.0000),
('FB', '2014-04-04 20:10:21', 39.0000),
('FB', '2014-04-04 20:10:28', 27.0000),
('FB', '2014-04-04 20:10:40', 32.0000),
('FB', '2014-04-04 20:10:45', 33.0000),
('FB', '2014-04-04 20:10:49', 32.0000),
('FB', '2014-04-04 20:10:55', 29.0000),
('FB', '2014-04-04 20:10:59', 24.0000),
('FB', '2014-04-04 20:11:06', 19.0000),
('FB', '2014-04-04 20:11:11', 18.0000),
('FB', '2014-04-04 20:11:16', 19.0000),
('FB', '2014-04-04 20:11:21', 22.0000),
('FB', '2014-04-04 20:11:26', 16.0000),
('FB', '2014-04-04 20:11:38', 11.0000),
('FB', '2014-04-04 20:11:43', 22.0000),
('FB', '2014-04-04 20:11:48', 66.0000),
('TSLA', '2014-04-01 23:10:52', 72.0000),
('TSLA', '2014-04-04 23:11:00', 62.0000);
所需的输出应
FB 66.0000 43
TSLA 62.0000 -10
答案 0 :(得分:1)
你的日期可能是一个字符串。试试看:
ORDER BY STR_TO_DATE(p1.DATE,'%Y-%m-%d %H:%m:%s') DESC,STR_TO_DATE(p2.DATE,'%Y-%m-%d %H:%m:%s') DESC
答案 1 :(得分:1)
您需要另一个自我加入才能获得最长日期
SELECT p1.ticker, p1.date, p1.close, p2.close c1,p1.close - p2.close
FROM prices AS p1
JOIN
(SELECT ticker,MAX(date) date
FROM prices
WHERE DATE(DATE ) = CURDATE( )
GROUP BY ticker)
pr1 USING(date,ticker)
LEFT JOIN prices AS p2 ON p1.ticker = p2.ticker
WHERE DATE( p2.DATE ) = CURDATE( ) -3
GROUP BY p1.ticker
ORDER BY p1.DATE DESC , p2.Date DESC ;
对于FB
组,差异将是44而不是43,因为从当前日期开始 - 3天最小关闭条目是22,所以今天的最近关闭条目是66和 - 22将是44而不是43
为了获得更佳的结果,您可以精确选择过去三天中最早的关闭条目
SELECT p1.ticker, p1.date, p1.close, p2.close c1,p1.close - p2.close
FROM prices AS p1
JOIN
(SELECT ticker,MAX(date) date
FROM prices
WHERE DATE(DATE ) = CURDATE( )
GROUP BY ticker) pr1 USING(date,ticker)
LEFT JOIN prices AS p2 ON p1.ticker = p2.ticker
JOIN
(SELECT ticker,MIN(date) date
FROM prices
WHERE DATE(DATE) = CURDATE() -3
GROUP BY ticker) pr2
ON(pr2.date=p2.date AND pr2.ticker=p2.ticker)
ORDER BY p1.DATE DESC , p2.Date DESC ;
答案 2 :(得分:0)
您可以单独执行限制以获得正确的结果,请参阅FIDDLE