SQL Server 计算不连续时间间隔

如图1:
SQL Server 计算不连续时间间隔_第1张图片

	SELECT shengChanHao AS shengchao,addDate 
	INTO #temp
	FROM packBanCPinUR WHERE 
	CONVERT(VARCHAR(10),addDate,23) BETWEEN convert(char(10),'2022-10-17',120) and convert(char(10),'2022-10-17',120) 
	AND classCode='L1'
	 GROUP BY shengChanHao,addDate ORDER BY addDate

SELECT shengchao, Convert(decimal(10,2),CONVERT(FLOAT,a.diff/60.0))  FROM 
(
	SELECT shengchao,sum(diff) as diff from (
     select shengchao, datediff(minute,min(adddate), max(adddate) ) as diff  ,(seqnum - seqnum2) as seqNum  from(
            select shengchao,adddate,row_number() over (order by adddate) as seqnum,
		        row_number() over (partition by shengchao order by adddate) as seqnum2 from #temp
   
		  )s	group by  shengchao ,(seqnum - seqnum2)
		)a group by shengchao
)a

最后的结果集:
SQL Server 计算不连续时间间隔_第2张图片

你可能感兴趣的:(SQL,数据库,sql,SQL,Server)