使用组更新行列表

时间:2014-10-07 15:57:55

标签: sql sql-server

我有一个"暂停"表和MesPieces表。当一个部分暂停时,我插入一个时间戳为Stop In时间的行,当这个部分暂停时,我插入Stop Out时间的时间戳。

当我更新MesPieces表中的时间时,我需要为每个MesPieces ID添加每个时差。

UPDATE Pause SET stopOut = CURRENT_TIMESTAMP FROM MesPieces
WHERE MesPieces.idMesPieces = Pause.idMesPieces
AND MesPieces.pauseAutomatique = 1
AND stopOut is NULL

UPDATE MesPieces set Statut = 'En cours', 
pauseAutomatique = 0,
tempsPause = DATEDIFF(MINUTE,stopIn,stopOut)
FROM Pause WHERE
Statut = 'Pause'
AND pauseAutomatique = 1
AND Pause.idMesPieces = MesPieces.idMesPieces

这是Pause表的一个例子

+----+-------------+-------------------------+-------------------------+
| id | idMesPieces |         stopIn          |         stopOut         |
+----+-------------+-------------------------+-------------------------+
| 12 |         123 | 2014-10-07 10:53:09.270 | 2014-10-07 11:23:10.140 |
| 13 |         123 | 2014-10-07 11:18:42.797 | 2014-10-07 11:23:10.140 |
| 14 |         124 | 2014-10-07 11:18:42.797 | 2014-10-07 11:23:10.140 |
| 15 |         125 | 2014-10-07 11:18:42.797 | 2014-10-07 11:23:10.140 |
| 16 |         126 | 2014-10-07 11:18:42.797 | 2014-10-07 11:23:10.140 |
| 17 |         123 | 2014-10-07 11:21:01.910 | 2014-10-07 11:23:10.140 |
| 18 |         124 | 2014-10-07 11:21:01.910 | 2014-10-07 11:23:10.140 |
| 19 |         125 | 2014-10-07 11:21:01.910 | 2014-10-07 11:23:10.140 |
| 20 |         126 | 2014-10-07 11:21:01.910 | 2014-10-07 11:23:10.140 |
| 21 |         123 | 2014-10-07 11:27:22.837 | 2014-10-07 11:27:38.623 |
| 22 |         124 | 2014-10-07 11:27:22.837 | 2014-10-07 11:27:38.623 |
| 23 |         125 | 2014-10-07 11:27:22.837 | 2014-10-07 11:27:38.623 |
| 24 |         126 | 2014-10-07 11:27:22.837 | 2014-10-07 11:27:38.623 |
+----+-------------+-------------------------+-------------------------+

这是MesPieces表

+-------------+-------+----------+-----------+--------------+--------------+-------+----------+--------+-------------------------+------------+------------------+
| idMesPieces | Poste |  Statut  | Quantitee | TempsUtilise | TempsRestant | idMo  | TempsStd | idUser |       timeOuvert        | tempsPause | pauseAutomatique |
+-------------+-------+----------+-----------+--------------+--------------+-------+----------+--------+-------------------------+------------+------------------+
|         122 | NULL  | Fin      | NULL      |          164 |         -154 | 10111 |       10 |      1 | 2014-10-07 08:31:58.103 |          0 |                0 |
|         123 | NULL  | En cours | NULL      |           34 |          -79 | 10106 |       15 |      1 | 2014-10-07 10:19:06.857 |         30 |                0 |
|         124 | NULL  | En cours | NULL      |            6 |            5 | 10105 |       15 |      1 | 2014-10-07 11:15:25.930 |          0 |                0 |
|         125 | NULL  | En cours | NULL      |            3 |            2 | 10109 |       15 |      1 | 2014-10-07 11:15:26.200 |          5 |                0 |
|         126 | NULL  | En cours | NULL      |            6 |            5 | 10108 |       15 |      1 | 2014-10-07 11:15:26.840 |          0 |                0 |
+-------------+-------+----------+-----------+--------------+--------------+-------+----------+--------+-------------------------+------------+------------------+

根据我的理解,问题是在我的更新中,当我添加DATEDIFF(MINUTE,stopIn,stopOut)我只添加第一场比赛时...我不知道如何添加全部它们。

1 个答案:

答案 0 :(得分:2)

不确定这是否有帮助,但您可以通过

加入群组查询
UPDATE MesPieces 
set 
    Statut = 'En cours', 
    pauseAutomatique = 0,
    tempsPause = Pause.duration
FROM MesPieces 
INNER JOIN (
    select 
        Pause.idMesPieces,
        SUM(DATEDIFF(MINUTE,stopIn,stopOut)) duration
    from Pause, MesPieces
    WHERE
        Statut = 'Pause' AND 
        pauseAutomatique = 1 AND
        Pause.idMesPieces = MesPieces.idMesPieces
    group by
        idMesPieces
) Pause on 
    Pause.idMesPieces = MesPieces.idMesPieces