У меня есть таблица, назовем их таблица SUMMARYDATA
NIP NAME DEPARTMENT STATUSIN STATUSOUT TOTALLOSTTIME
------------------------------------------------------------------------------------------------
A1 ARIA BB 2020-01-21 08:06:23 2020-01-21 11:58:36 01:05:00
A1 ARIA BB 2020-01-22 07:34:27 2020-01-22 17:19:47 00:30:00
A1 ARIA BB 2020-01-23 08:30:00 2020-01-23 11:00:00 00:00:00
A1 ARIA BB 2020-01-24 08:05:00 2020-01-24 10:30:00 01:00:00
A2 BELLE BB 2020-01-21 07:06:20 2020-01-21 13:58:31 00:30:00
A2 BELLE BB 2020-01-22 07:34:27 2020-01-22 17:19:47 00:15:00
A2 BELLE BB 2020-01-23 07:06:00 2020-01-23 10:30:00 00:00:00
A2 BELLE BB 2020-01-24 09:06:00 2020-01-23 10:30:00 00:05:00
A3 CHLOE CC 2020-01-21 07:06:23 2020-01-21 11:55:30 00:30:00
A3 CHLOE CC 2020-01-22 07:34:27 2020-01-22 17:00:44 01:00:00
A3 CHLOE CC 2020-01-23 08:37:00 2020-01-23 11:13:00 00:35:00
A3 CHLOE CC 2020-01-24 08:09:00 2020-01-24 10:22:00 00:00:00
A4 ZIYA CC 2020-01-21 07:06:20 2020-01-21 13:58:31 00:30:00
A4 ZIYA CC 2020-01-22 07:34:27 2020-01-22 17:19:47 00:15:00
A4 ZIYA CC 2020-01-23 06:06:00 2020-01-23 11:30:00 00:45:00
A4 ZIYA CC 2020-01-24 09:06:00 2020-01-23 15:30:00 00:00:00
A5 BRIAN BB 2020-01-21 08:06:23 2020-01-21 11:58:36 01:10:00
A5 BRIAN BB 2020-01-22 07:34:27 2020-01-22 17:19:47 01:00:00
A5 BRIAN BB 2020-01-23 08:30:00 2020-01-23 11:00:00 00:30:00
A5 BRIAN BB 2020-01-24 08:05:00 2020-01-24 10:30:00 00:10:00
Мне нужно SELECT NIP,NAME,DEPARTMENT,STATUSIN,TOTALLOSTTIME
в месяц и в год where sum(TOTALLOSTTIME) > '02:00:00'
. Тогда столбец типа данных в TOTALLOSTTIME
равен nvarchar
И это результат, который я имел в виду:
NIP NAME DEPARTMENT STATUSIN STATUSOUT TOTALLOSTTIME
------------------------------------------------------------------------------------------------
A1 ARIA BB 2020-01-21 08:06:23 2020-01-21 11:58:36 01:05:00
A1 ARIA BB 2020-01-22 07:34:27 2020-01-22 17:19:47 00:30:00
A1 ARIA BB 2020-01-24 08:05:00 2020-01-24 10:30:00 01:00:00
A3 CHLOE CC 2020-01-21 07:06:23 2020-01-21 11:55:30 00:30:00
A3 CHLOE CC 2020-01-22 07:34:27 2020-01-22 17:00:44 01:00:00
A3 CHLOE CC 2020-01-23 08:37:00 2020-01-23 11:13:00 00:35:00
A5 BRIAN BB 2020-01-21 08:06:23 2020-01-21 11:58:36 01:10:00
A5 BRIAN BB 2020-01-22 07:34:27 2020-01-22 17:19:47 01:00:00
A5 BRIAN BB 2020-01-23 08:30:00 2020-01-23 11:00:00 00:30:00
A5 BRIAN BB 2020-01-24 08:05:00 2020-01-24 10:30:00 00:10:00
И я пробовал этот запрос до сих пор:
select NIP,NAME,DEPARTMENT,x.sum_lost_time,x.months,x.years
from (select MONTH(STATUSIN) as [months]
,YEAR(STATUSIN) as [years]
,NIP
,NAME
,DEPARTMENT
,convert(varchar,dateadd(second,sum(datediff(second,'00:00:00',cast(TOTALLT as time))),0),108) as sum_lost_time
from SUMMARYDATA b
group by MONTH(STATUSIN)
,YEAR(STATUSIN)
,NIP
,NAME
,DEPARTMENT
)x
WHERE x.sum_lost_time > '10:00:00'