请通过以下查询创建表:
CREATE TABLE `trade`
(
`order` VARCHAR(10) DEFAULT NULL,
`positionid` INT(11) DEFAULT NULL,
`time` DATETIME DEFAULT NULL,
`volume` FLOAT NOT NULL
)
engine=innodb
DEFAULT charset=latin1;
插入查询
INSERT INTO `trade`
(`order`,
`positionid`,
`time`,
`volume`)
VALUES ('42556',
1111,
'2018-08-15 07:27:44',
2),
('42560',
1111,
'2018-08-18 08:32:47',
2),
('42564',
1235,
'2018-08-21 07:10:12',
5),
('42572',
1235,
'2018-08-23 17:20:26',
2),
('42580',
1235,
'2018-08-23 17:03:30',
3);
我已经尝试过以下查询:
SELECT b.`order` AS `TICKET`,
b.`time` AS `OPEN_TIME`,
j.`time` AS `CLOSE_TIME`
FROM trade AS b
LEFT JOIN trade AS j
ON b.`positionid` = j.`positionid`
WHERE b.`time` != j.`time`;
表格:
错误输出:
此图显示重复数据:
必需的输出:
此图将显示所需的输出:
说明:
当我们打开交易时,它将存储到交易表中。之后,当我们关闭交易时,它将执行另一次具有不同时间和数量的输入(如果交易将部分关闭,则交易量将不同,否则将完全关闭交易)。 在这里,第一个条目将是OPEN_TIME(存储为时间),第二个条目将是CLOSE_TIME。那么,如何使用OPEN_TIME和CLOSE_TIME将两个或更多记录转换为单个记录?
答案 0 :(得分:1)
检查
SELECT j.`order` AS `TICKET`,
b.`positionid` AS `PositionID`,
b.`time` AS `OPEN_TIME`,
j.`time` AS `CLOSE_TIME` ,
j.`volume` AS `Volume`
FROM trade AS b
INNER JOIN trade AS j
ON b.`positionid` = j.`positionid`
where b.time = (select min(time) from trade as c1 where b.positionid=c1.positionid)
AND b.time!=j.time;
答案 1 :(得分:0)
将时间条件更改为:
-编辑
where j.time = (select max(time) from test_query where positionid = j.positionid)
and b.time = (select min(time) from test_query where positionid = b.positionid)
以使b
实例等同于未结订单,j
实例等同于未结订单