返回时间戳的两个日期之间的datediff

polkgigr  于 2021-08-13  发布在  Java
关注(0)|答案(3)|浏览(421)

解决了的

CONCAT((DATEDIFF(Minute,START_DTTM,END_DTTM)/60),'h:',
       (DATEDIFF(Minute,START_DTTM,END_DTTM)%60) 'm') AS TotalTimeMissing

生成totaltimemissing:5h:13m


我试图返回两个特定日期之间的时间戳值,以计算包丢失和被找到之间的时间。
编辑:代码已更新为包含来自sami的代码。我还添加了从原始代码中排除的其他代码。
以下是当前代码:

USE PACKAGE

GO  

SELECT

        dp.LEGACY_ID

       ,dp.SURNAME

       ,dp.FORENAME

       ,dp.ETHNICITY_DESCRIPTION

       ,dp.BIRTH_DTTM

       ,DATEDIFF(YY, dp.BIRTH_DTTM, GETDATE()) -

            CASE

                  WHEN RIGHT(CONVERT(VARCHAR(6), GETDATE(), 12), 4) >=

                        RIGHT(CONVERT(VARCHAR(6), dp.BIRTH_DTTM, 12), 4)

                        THEN 0

                  ELSE 1

            END AS [Current Age]

--^Precise age calc due to potential LL inaccuracy

       ,mp.DIM_PERSON_ID

       ,mp.MISSING_STATUS

       ,mp.START_DTTM

       ,mp.END_DTTM

       ,dp.LEGACY_ID

       ,mp.RETURN_INT_OFFERED

       ,mp.RETURN_INT_ACCEPTED

       ,mp.RETURN_INT_DATE

FROM C_S.FACT_MISSING_PACKAGE AS mp

JOIN C_S.FACT_MISSING_PACKAGE AS dp ON mp.DIM_PERSON_ID = dp.DIM_PERSON_ID

WHERE CAST (mp.START_DTTM AS DATE)

              BETWEEN DATEADD(YY, -1, CAST (GETDATE() AS DATE)) AND CAST (GETDATE() AS DATE)

--^Displays all records within exactly 1 year of run date

       UNION (SELECT CONCAT(Value / 3600 / 24,

              ' Days ',

              RIGHT(CONCAT('00', Value / 3600 % 24), 2),

              ':',

              RIGHT(CONCAT('00', Value / 60 % 60), 2),

              ':',

              RIGHT(CONCAT('00',Value % 3600 % 60), 2)

       ) AS TotalTimeMissing

FROM

(

  SELECT mp.DIM_PERSON_ID, DATEDIFF(Second, mp.START_DTTM, mp.END_DTTM) Value

  FROM C_S.FACT_MISSING_PACKAGE AS mp

) T(Value))

ORDER BY START_DTTM ASC;

sami让我大部分时间都在那里,但是当我运行上面的代码时,我得到了一个关于union和t的错误,t没有说明所需的列数。为了解决这个问题,我尝试将第一轮select列放入(select concat()语句中,但它会生成错误,所以在如何修复它时我有点不知所措?
我需要返回所有这些列,并在末尾添加一列totaltimemissing
谢谢

kyxcudwk

kyxcudwk1#

您可以先使用 DATEDIFF 函数,然后计算小时和天数,知道1小时是60分钟,1天是1440分钟。
要知道 DATEDIFF 在sql server中工作:
此函数返回指定的开始日期和结束日期之间的指定日期部分边界的计数(作为有符号整数值)。
所以,

DATEDIFF(day, '2020-01-13 23:59:58', '2020-01-14 00:00:08')

将返回1,即使差值只有几秒钟,因为给定的间隔跨越了一天的边界(午夜)。
所以你不应该使用 DATEDIFF(day, ...) 在这里,但使用 DATEDIFF(minute, ...) 或者 DATEDIFF(second, ...) 并根据经过的分或秒的总数计算小时数和天数。
我将使用 CROSS APPLY 避免多次键入长表达式。我也使用整数除法 / 在这里,它丢弃了小数部分,例如。 200 / 60 = 3 .

Total days = total minutes / 1440 (discard fractional part)
Total hours = total minutes / 60 (discard fractional part)

但是,我们不需要总小时数,我们需要的是总天数之后剩下的额外小时数,所以我们需要使用mod 24。

Hours = Total hours % 24

对于最后几分钟,我们只需要总天数和总小时数之后的剩余几分钟,因此

Minutes = total minutes mod 60.

查询:

SELECT 
     dp.LEGACY_ID
    ,dp.SURNAME
    ,dp.FORENAME
    ,mp.DIM_PERSON_ID
    ,mp.MISSING_STATUS
    ,mp.START_DTTM
    ,mp.END_DTTM
    ,dp.LEGACY_ID
    ,STR(DiffMinutes / 1440) + ':' +        -- total days
     STR(DiffMinutes / 60 % 24) + ':' +     -- hours (0 .. 23)
     STR(DiffMinutes % 60) AS TimeMissing   -- minutes (0 .. 59)
FROM 
    MissingPackages AS mp
    JOIN DIM_PERSON AS dp ON mp.DIM_PERSON_ID = dp.DIM_PERSON_ID
    CROSS APPLY
    (
        SELECT DATEDIFF(minute, mp.START_DTTM, mp.END_DTTM) AS DiffMinutes
    ) AS A
ORDER BY START_DTTM ASC;
kxe2p93d

kxe2p93d2#

CONCAT(DATEDIFF(day, START_DT, END_DT), '-', DATEDIFF(hour, START_DT, END_DT), '-', DATEDIFF(minute, START_DT, END_DT)) AS TimeMissing
2uluyalo

2uluyalo3#

这就是你要找的吗

CREATE TABLE MyData 
(
  StartDate DATETIME, 
  EndDate DATETIME
);

INSERT INTO MyData VALUES
('2017-01-01 00:00:00', '2018-01-02 00:25:01'),
('2017-01-01 00:00:00', '2018-01-01 00:00:00'),
('2017-01-02 12:00:09', '2017-01-02 12:00:30'),
('2017-01-01 02:00:00', '2017-01-01 03:30:30'),
('2017-01-01 00:00:00', '2017-01-03 00:30:30'),
('2017-12-31 23:59:59', '2018-01-01 00:00:01'),
('2017-12-31 23:59:01', '2018-01-01 00:00:01');

SELECT CONCAT(Value / 3600 / 24, 
              ' Days ', 
              RIGHT(CONCAT('00', Value / 3600 % 24), 2), 
              ':', 
              RIGHT(CONCAT('00', Value / 60 % 60), 2), 
              ':',
              RIGHT(CONCAT('00',Value % 3600 % 60), 2)
       ) TimeMissing 
FROM
(
  SELECT DATEDIFF(Second, StartDate, EndDate) Value
  FROM MyData
) T(Value);

退货:

+-------------------+
|    TimeMissing    |
+-------------------+
| 366 Days 00:25:01 |
| 365 Days 00:00:00 |
| 0 Days 00:00:21   |
| 0 Days 01:30:30   |
| 2 Days 00:30:30   |
| 0 Days 00:00:02   |
| 0 Days 00:01:00   |
+-------------------+

相关问题