T-SQL脚本用于计算多行之间的经过时间

时间:2017-10-06 00:15:37

标签: sql sql-server

下午好,我试图在sql中生成一个查询,以了解每个服务订单使用的时间跨度。 任何帮助表示赞赏。

示例:

CITA    FECHACITA   MANODEOBRA  UNIDTIEMPO  MECANICO
261354  06/10/2017  M-M.O. 0 MANTENIMIENTO  09:30   10 LUIS ENRIQUE HERRERA 
MARTINEZ 
261354  06/10/2017  M-M.O. 0 MANTENIMIENTO  09:35   10 LUIS ENRIQUE HERRERA 
MARTINEZ 
261354  06/10/2017  M-M.O. 0 MANTENIMIENTO  09:40   10 LUIS ENRIQUE HERRERA 
MARTINEZ 
261354  06/10/2017  M-M.O. 0 MANTENIMIENTO  09:45   10 LUIS ENRIQUE HERRERA 
MARTINEZ 
261354  06/10/2017  M-M.O. 0 MANTENIMIENTO  09:50   10 LUIS ENRIQUE HERRERA 
MARTINEZ 
261354  06/10/2017  M-M.O. 0 MANTENIMIENTO  09:55   10 LUIS ENRIQUE HERRERA 
MARTINEZ 
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    08:30   02 ERIK BENJAMIN 
GONZALEZ GODINEZ 
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    08:35   02 ERIK BENJAMIN 
GONZALEZ GODINEZ 
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    08:40   02 ERIK BENJAMIN 
GONZALEZ GODINEZ 
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    08:45   02 ERIK BENJAMIN 
GONZALEZ GODINEZ 
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    08:50   02 ERIK BENJAMIN 
GONZALEZ GODINEZ 
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    08:55   02 ERIK BENJAMIN 
GONZALEZ 
GODINEZ 
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    09:00   04 JUAN CARLOS MONTAÑEZ
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    09:05   04 JUAN CARLOS MONTAÑEZ
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    09:10   04 JUAN CARLOS MONTAÑEZ

所以应该看到

CITA    FECHACITA   MANODEOBRA  UNIDTIEMPO  MECANICO
261354  06/10/2017  M-M.O. 0 MANTENIMIENTO  00:25   10 LUIS ENRIQUE HERRERA 
MARTINEZ 
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    00:25   02 ERIK BENJAMIN 
GONZALEZ GODINEZ 
261858  06/10/2017  M-M.O. 2 DIAGNOSTICO    00:10   04 JUAN CARLOS MONTAÑEZ

1 个答案:

答案 0 :(得分:0)

您可以使用if (Build.VERSION.SDK_INT < 21) { CompoundButtonCompat.setButtonTintList(button, ColorStateList.valueOf(tintColor));//Use android.support.v4.widget.CompoundButtonCompat when necessary else } else { button.setButtonTintList(ColorStateList.valueOf(tintColor));//setButtonTintList is accessible directly on API>19 } 为每一行获取下一次(如果存在)。请注意,我们使用LAG子句进行分组:

PARTITION BY

enter image description here

我们正在使用DECLARE @DataSource TABLE ( [CITA] INT ,[FECHACITA] DATE ,[MANODEOBRA] VARCHAR(32) ,[UNIDTIEMPO] TIME ,[MECANICO] VARCHAR(64) ); INSERT INTO @DataSource ([CITA], [FECHACITA], [MANODEOBRA], [UNIDTIEMPO], [MECANICO]) VALUES ('261354', '06/10/2017', 'M-M.O. 0 MANTENIMIENTO', '09:30', '10 LUIS ENRIQUE HERRERA MARTINEZ') ,('261354', '06/10/2017', 'M-M.O. 0 MANTENIMIENTO', '09:35', '10 LUIS ENRIQUE HERRERA MARTINEZ') ,('261354', '06/10/2017', 'M-M.O. 0 MANTENIMIENTO', '09:40', '10 LUIS ENRIQUE HERRERA MARTINEZ') ,('261354', '06/10/2017', 'M-M.O. 0 MANTENIMIENTO', '09:45', '10 LUIS ENRIQUE HERRERA MARTINEZ') ,('261354', '06/10/2017', 'M-M.O. 0 MANTENIMIENTO', '09:50', '10 LUIS ENRIQUE HERRERA MARTINEZ') ,('261354', '06/10/2017', 'M-M.O. 0 MANTENIMIENTO', '09:55', '10 LUIS ENRIQUE HERRERA MARTINEZ') ,('261858', '06/10/2017', 'M-M.O. 2 DIAGNOSTICO', '08:30', '02 ERIK BENJAMIN GONZALEZ GODINEZ') ,('261858', '06/10/2017', 'M-M.O. 2 DIAGNOSTICO', '08:35', '02 ERIK BENJAMIN GONZALEZ GODINEZ') ,('261858', '06/10/2017', 'M-M.O. 2 DIAGNOSTICO', '08:40', '02 ERIK BENJAMIN GONZALEZ GODINEZ') ,('261858', '06/10/2017', 'M-M.O. 2 DIAGNOSTICO', '08:45', '02 ERIK BENJAMIN GONZALEZ GODINEZ') ,('261858', '06/10/2017', 'M-M.O. 2 DIAGNOSTICO', '08:50', '02 ERIK BENJAMIN GONZALEZ GODINEZ') ,('261858', '06/10/2017', 'M-M.O. 2 DIAGNOSTICO', '08:55', '02 ERIK BENJAMIN GONZALEZ GODINEZ') ,('261858', '06/10/2017', 'M-M.O. 2 DIAGNOSTICO', '09:00', '04 JUAN CARLOS MONTAÑEZ') ,('261858', '06/10/2017', 'M-M.O. 2 DIAGNOSTICO', '09:05', '04 JUAN CARLOS MONTAÑEZ') ,('261858', '06/10/2017', 'M-M.O. 2 DIAGNOSTICO', '09:10', '04 JUAN CARLOS MONTAÑEZ'); SELECT [CITA], [FECHACITA], [MANODEOBRA], [MECANICO] ,DATEDIFF(MINUTE, [UNIDTIEMPO], LEAD([UNIDTIEMPO], 1, NULL) OVER (PARTITION BY [CITA], [FECHACITA], [MANODEOBRA], [MECANICO] ORDER BY [UNIDTIEMPO])) AS [TimeInMinutes] FROM @DataSource ORDER BY [FECHACITA] ,[UNIDTIEMPO]; 函数在几分钟内获得时差。得到这个结果,剩下的就是简单地总结每个小组的时间:

DATEDIFF

enter image description here

请注意,您没有指定WITH DataSource AS ( SELECT [CITA], [FECHACITA], [MANODEOBRA], [MECANICO] ,DATEDIFF(MINUTE, [UNIDTIEMPO], LEAD([UNIDTIEMPO], 1, NULL) OVER (PARTITION BY [CITA], [FECHACITA], [MANODEOBRA], [MECANICO] ORDER BY [UNIDTIEMPO])) AS [TimeInMinutes] FROM @DataSource ) SELECT [CITA], [FECHACITA], [MANODEOBRA], [MECANICO] ,CAST(CONCAT(SUM([TimeInMinutes]) / 60 , ':', SUM([TimeInMinutes]) % 60) AS TIME) AS UNIDTIEMPO FROM DataSource GROUP BY [CITA], [FECHACITA], [MANODEOBRA], [MECANICO]; 列的SQL数据类型,因此我正在使用时间。如果您将其存储为字符串,则需要将其转换为UNIDTIEMPO

对于time,您可以使用以下查询:

SQL Server 2008 R2