1

基本的に、「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

これらのデータを2番目のテーブル「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 Compeleted

誰かが私がこれのためのSQLクエリを書くのを手伝ってくれるなら、私はそれを高く評価します。前もって感謝します

4

4 に答える 4

2
INSERT INTO t2 (ID, STATUS, TIME)
SELECT ID, STATUS, MIN(TIME) FROM t1 t1top
WHERE EXISTS(SELECT * FROM t1 WHERE ID=t1top.ID AND STATUS='Complete')
GROUP BY ID, STATUS
ORDER BY CAST(ID AS UNSIGNED) ASC, STATUS DESC

挿入が行われた後、例に従って結果を確認したい場合は、次の選択を実行する必要があります。

SELECT ID, STATUS, TIME FROM table_1
ORDER BY CAST(ID AS UNSIGNED) ASC, STATUS DESC

それはまさに正しいですが、そのように見たくありません。2番目のテーブルにもそのように挿入する必要があります。

于 2012-05-31T23:40:21.630 に答える
1
INSERT INTO table_2
SELECT id,status,min(time)
FROM table_1 AS t1
WHERE EXISTS(SELECT 1
             FROM table_1
             WHERE id=t1.id
                 AND status='complete')
GROUP BY id,status

私はそれがあなたのためにそれを行うべきだと思いますが、それをテストしていません:(

于 2012-05-31T23:37:30.010 に答える
0

次のようにすべての開始時間を取得できます。

   select id, status, min(time) 
   from table_1 
   where status = 'Pending'
   group by id, status

次に、次のように完了します。

   select id, status, time
   from table_1 
   where status = 'Complete'
   group by id, status

ユニオンを使用して両方を使用できます。もちろん、次を試してください。

   insert into table_2
于 2012-06-01T00:00:55.840 に答える
0
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
于 2012-06-02T23:24:57.433 に答える