加入小时和分钟列以十进制格式显示 SQL 查询
Join Hours and Minute column for showing in Decimal format SQL Query
我在 SQL 服务器中有两个 datetime
列 table:
+--------------------------+------------------------+
| CompLast_SignON_datetime | comp_accidentdate_time |
+--------------------------+------------------------+
| 16-12-2021 04:08 | 16-12-2021 05:10 |
| 17-12-2021 14:20 | 17-12-2021 20:00 |
| 18-12-2021 12:15 | 18-12-2021 15:25 |
| 22-12-2021 06:00 | 22-12-2021 12:34 |
| 25-12-2021 11:30 | 25-12-2021 21:40 |
| 26-12-2021 21:00 | 27-12-2021 02:50 |
+--------------------------+------------------------+
我通过SQL查询
从中分离出小时和分钟
SELECT
CompLast_SignON_datetime, comp_accidentdate_time,
CONVERT(INT, ROUND((DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) / 60), 2, 1)) AS 'Hours',
FORMAT(CONVERT(INT, DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) - CONVERT(INT, (ROUND((DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) / 60), 2, 1)) * 60)), '00') AS 'Minutes'
FROM Safety_SIMS
输出为
+--------------------------+------------------------+-------+---------+
| CompLast_SignON_datetime | comp_accidentdate_time | Hours | Minutes |
+--------------------------+------------------------+-------+---------+
| 16-12-2021 04:08 | 16-12-2021 05:10 | 1 | 02 |
| 17-12-2021 14:20 | 17-12-2021 20:00 | 5 | 40 |
| 18-12-2021 12:15 | 18-12-2021 15:25 | 3 | 10 |
| 22-12-2021 06:00 | 22-12-2021 12:34 | 6 | 34 |
| 25-12-2021 11:30 | 25-12-2021 21:40 | 10 | 10 |
| 26-12-2021 21:00 | 27-12-2021 02:50 | 5 | 50 |
+--------------------------+------------------------+-------+---------+
现在,为了进一步分组数据,我需要在一列中加入小时和分钟列以获得这样的输出
+--------------------------+------------------------+------------+
| CompLast_SignON_datetime | comp_accidentdate_time | Duty_hours |
+--------------------------+------------------------+------------+
| 16-12-2021 04:08 | 16-12-2021 05:10 | 1.02 |
| 17-12-2021 14:20 | 17-12-2021 20:00 | 5.40 |
| 18-12-2021 12:15 | 18-12-2021 15:25 | 3.10 |
| 22-12-2021 06:00 | 22-12-2021 12:34 | 6.34 |
| 25-12-2021 11:30 | 25-12-2021 21:40 | 10.10 |
| 26-12-2021 21:00 | 27-12-2021 02:50 | 5.50 |
+--------------------------+------------------------+------------+
您可以尝试将小时和分钟都转换为 varchar 并将两者连接起来以创建新列。
SELECT
CompLast_SignON_datetime, comp_accidentdate_time,
CONVERT(VARCHAR(5), ROUND((DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) / 60), 2, 1)) + '.' +
CONVERT(VARCHAR(5), FORMAT(CONVERT(INT, DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) - CONVERT(INT,(ROUND((DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) / 60), 2, 1)) * 60)), '00')) AS 'Duty_hours'
FROM Safety_SIMS
由于您显示的是分钟而不是小时的一小部分,因此使用 :
而不是 .
会更自然
然后您可以使用 DATEDIFF 和 FORMAT 将小时和分钟的差异作为字符串。
select *
, [Duty_hours] = format(datediff(hour, CompLast_SignON_datetime, comp_accidentdate_time), '00')
+ format(comp_accidentdate_time - CompLast_SignON_datetime, ':mm')
from Safety_SIMS;
CompLast_SignON_datetime
comp_accidentdate_time
Duty_hours
2021-12-16 04:08
2021-12-16 05:10
01:02
2021-12-17 14:20
2021-12-17 20:00
06:40
2021-12-18 12:15
2021-12-18 15:25
03:10
2021-12-22 06:00
2021-12-22 12:34
06:34
2021-12-25 11:30
2021-12-25 21:40
10:10
2021-12-26 21:00
2021-12-27 02:50
05:50
演示 db<>fiddle here
可以简化
select *
, [Duty_hours] = format(comp_accidentdate_time - CompLast_SignON_datetime, 'HH:mm')
from Safety_SIMS;
但是您假设差异总是小于一天。
这是一个将时差计算为小数的版本。
select *
, [Duty_hours] = datediff(hour, CompLast_SignON_datetime, comp_accidentdate_time)
+ cast(format(comp_accidentdate_time - CompLast_SignON_datetime, '.mm') as decimal(10,2))
from Safety_SIMS;
我在 SQL 服务器中有两个 datetime
列 table:
+--------------------------+------------------------+
| CompLast_SignON_datetime | comp_accidentdate_time |
+--------------------------+------------------------+
| 16-12-2021 04:08 | 16-12-2021 05:10 |
| 17-12-2021 14:20 | 17-12-2021 20:00 |
| 18-12-2021 12:15 | 18-12-2021 15:25 |
| 22-12-2021 06:00 | 22-12-2021 12:34 |
| 25-12-2021 11:30 | 25-12-2021 21:40 |
| 26-12-2021 21:00 | 27-12-2021 02:50 |
+--------------------------+------------------------+
我通过SQL查询
从中分离出小时和分钟SELECT
CompLast_SignON_datetime, comp_accidentdate_time,
CONVERT(INT, ROUND((DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) / 60), 2, 1)) AS 'Hours',
FORMAT(CONVERT(INT, DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) - CONVERT(INT, (ROUND((DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) / 60), 2, 1)) * 60)), '00') AS 'Minutes'
FROM Safety_SIMS
输出为
+--------------------------+------------------------+-------+---------+
| CompLast_SignON_datetime | comp_accidentdate_time | Hours | Minutes |
+--------------------------+------------------------+-------+---------+
| 16-12-2021 04:08 | 16-12-2021 05:10 | 1 | 02 |
| 17-12-2021 14:20 | 17-12-2021 20:00 | 5 | 40 |
| 18-12-2021 12:15 | 18-12-2021 15:25 | 3 | 10 |
| 22-12-2021 06:00 | 22-12-2021 12:34 | 6 | 34 |
| 25-12-2021 11:30 | 25-12-2021 21:40 | 10 | 10 |
| 26-12-2021 21:00 | 27-12-2021 02:50 | 5 | 50 |
+--------------------------+------------------------+-------+---------+
现在,为了进一步分组数据,我需要在一列中加入小时和分钟列以获得这样的输出
+--------------------------+------------------------+------------+
| CompLast_SignON_datetime | comp_accidentdate_time | Duty_hours |
+--------------------------+------------------------+------------+
| 16-12-2021 04:08 | 16-12-2021 05:10 | 1.02 |
| 17-12-2021 14:20 | 17-12-2021 20:00 | 5.40 |
| 18-12-2021 12:15 | 18-12-2021 15:25 | 3.10 |
| 22-12-2021 06:00 | 22-12-2021 12:34 | 6.34 |
| 25-12-2021 11:30 | 25-12-2021 21:40 | 10.10 |
| 26-12-2021 21:00 | 27-12-2021 02:50 | 5.50 |
+--------------------------+------------------------+------------+
您可以尝试将小时和分钟都转换为 varchar 并将两者连接起来以创建新列。
SELECT
CompLast_SignON_datetime, comp_accidentdate_time,
CONVERT(VARCHAR(5), ROUND((DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) / 60), 2, 1)) + '.' +
CONVERT(VARCHAR(5), FORMAT(CONVERT(INT, DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) - CONVERT(INT,(ROUND((DATEDIFF(minute, CompLast_SignON_datetime, comp_accidentdate_time) / 60), 2, 1)) * 60)), '00')) AS 'Duty_hours'
FROM Safety_SIMS
由于您显示的是分钟而不是小时的一小部分,因此使用 :
而不是 .
然后您可以使用 DATEDIFF 和 FORMAT 将小时和分钟的差异作为字符串。
select * , [Duty_hours] = format(datediff(hour, CompLast_SignON_datetime, comp_accidentdate_time), '00') + format(comp_accidentdate_time - CompLast_SignON_datetime, ':mm') from Safety_SIMS;
CompLast_SignON_datetime | comp_accidentdate_time | Duty_hours |
---|---|---|
2021-12-16 04:08 | 2021-12-16 05:10 | 01:02 |
2021-12-17 14:20 | 2021-12-17 20:00 | 06:40 |
2021-12-18 12:15 | 2021-12-18 15:25 | 03:10 |
2021-12-22 06:00 | 2021-12-22 12:34 | 06:34 |
2021-12-25 11:30 | 2021-12-25 21:40 | 10:10 |
2021-12-26 21:00 | 2021-12-27 02:50 | 05:50 |
演示 db<>fiddle here
可以简化
select *
, [Duty_hours] = format(comp_accidentdate_time - CompLast_SignON_datetime, 'HH:mm')
from Safety_SIMS;
但是您假设差异总是小于一天。
这是一个将时差计算为小数的版本。
select *
, [Duty_hours] = datediff(hour, CompLast_SignON_datetime, comp_accidentdate_time)
+ cast(format(comp_accidentdate_time - CompLast_SignON_datetime, '.mm') as decimal(10,2))
from Safety_SIMS;