可能重复:
如何选择在 sql 中插入具有相同 id 的行?
我有一个名为“table_1”的表:
ID Index STATUS TIME DESCRIPTION
1 15 pending 1:00 Started Pending
1 16 pending 1:05 still in request
1 17 pending 1:10 still in request
1 18 complete 1:20 Transaction has been completed
2 19 pending 2:25 request has been started
2 20 pending 2:30 in progress
2 21 pending 2:35 in progess still
2 22 pending 2:40 still pending
2 23 complete 2:45 Transaction Compeleted
我需要将这些数据插入到我的第二个表“table_2”中,其中只包括开始时间和完成时间,所以我的“table_2”应该是这样的:
ID Index STATUS TIME DESCRIPTION
1 15 pending 1:00 Started Pending
1 18 complete 1:20 Transaction has been completed
2 19 pending 2:25 request has been started
2 23 complete 2:45 Transaction Completed
这是我已经拥有的一些代码,我无法获得正确的关联描述,因为它没有出现在GROUP BY
子句中。
INSERT INTO myTable2 (ID, STATUS, TIME)
SELECT ID, STATUS, MIN(TIME) FROM myTable1 t1top
WHERE EXISTS(SELECT * FROM myTable1 WHERE ID=t1top.ID AND STATUS='Complete')
GROUP BY ID, STATUS
ORDER BY ID ASC, STATUS DESC
如果有人可以帮助我为此编写 sql 查询,我将不胜感激。