如何在sql中选择distinct select?

时间:2012-06-02 22:38:59

标签: sql ssis

  

可能重复:
  how to choose which row to insert with same id in sql?

我有一个名为“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查询,我会非常感激。

4 个答案:

答案 0 :(得分:1)

INSERT INTO myTable2 (ID, STATUS, TIME, DESCRIPTION)
SELECT t1.ID, t1.STATUS, t1.TIME, t1.DESCRIPTION FROM table_1 as t1 
WHERE STATUS = 'complete' 
OR TIME = (SELECT min(TIME) FROM table_1 WHERE ID = t1.ID) 
ORDER BY ID asc, STATUS desc

答案 1 :(得分:0)

您可以自行加入表并从中进行选择,假设时间戳是唯一的。 (如果不是,则说明不是由您的数据唯一定义的。)

INSERT INTO myTable2 (ID, STATUS, TIME, DESCRIPTION)
SELECT t1top.ID, t1top.STATUS, MIN(t1top.TIME), t1Copy.DESCRIPTION
FROM myTable1 t1top
    JOIN myTable1 t1Copy ON t1top.ID = t1Copy.ID
WHERE EXISTS(SELECT * FROM myTable1 WHERE ID=t1top.ID AND STATUS='Complete')
    AND MIN(t1top.TIME) = t1Copy.TIME
GROUP BY ID, STATUS
ORDER BY ID ASC, STATUS DESC

答案 2 :(得分:0)

尝试这样的事情:

SELECT * FROM myTable1 WHERE DESCRIPTION = '%started%' OR DESCRIPTION = '%complete%' ORDER BY ID, STATUS

答案 3 :(得分:0)

您可以使用分区功能:

SELECT a.ID, a.Index, a.STATUS, a.TIME, a.DESCRIPTION FROM
(
    SELECT ID, Index, STATUS, TIME, DESCRIPTION, 
    MIN(Index) OVER (PARTITION BY ID) AS first_index,
    MAX(CASE WHEN STATUS = 'complete' then 1 else 0 end) OVER (PARTITION BY ID) AS is_complete
   FROM myTable1
) a
WHERE a.is_complete = 1 AND (a.status = 'complete' OR a.index = a.first_index)

换句话说,选择已完成的第一行和最后一行事件。