简体   繁体   English

计算两个月之间的持续时间(超过两个月)(拆分)

[英]Calculating duration between two dates which go over months (splitting)

I'm looking at calculating the difference between two date fields in SQL (SQL Server) which may (or may not) overlap to another month. 我正在计算SQL(SQL Server)中两个日期字段之间的差异,该日期可能(也可能不)重叠到另一个月。

I have a TimeFrom field and a TimeTo field and a status field. 我有一个TimeFrom字段,一个TimeTo字段和一个状态字段。 Both of the Time fields are in datetime format and the status is numeric. 两个“时间”字段均为日期时间格式,状态为数字。

Here is an example of the dates: 以下是日期的示例:

TimeFrom               TimeTo                Status
2018-04-01 09:03:27    2018-07-15 10:11:13   12

I need to be calculate the time between those dates but in the relevant month and I'm struggling to work this out myself. 我需要计算出这两个日期之间的时间,但要计算相关的月份,而我正在努力自己解决这个问题。

Output would look something like: 输出如下所示:

YearMonth | Status | Duration (hh:mm:ss)
2018-04 | 12      | xx:xx:xx
2018-05 | 12 | xx:xx:xx
2018-06 | 12 | xx:xx:xx

this should work: 这应该工作:

set serveroutput on;
create or replace procedure d061(d1  in  date, d2  in date ) as
dd number;
hh24 number;
mi number;
ss number;
begin
select extract(day from intrvl) into  dd
    from (
          select   to_timestamp_tz (d1,'DD MON YYYY hh24:mi:ss')
                 - to_timestamp_tz (d2,'DD MON YYYY hh24:mi:ss') as intrvl
         from dual
         );


select  extract(hour from intrvl) into hh24
        from (
          select   to_timestamp_tz (d1,'DD MON YYYY hh24:mi:ss')
                 - to_timestamp_tz (d2,'DD MON YYYY hh24:mi:ss') as intrvl
         from dual
         );
select  extract(minute from intrvl) into mi

        from (
          select   to_timestamp_tz (d1,'DD MON YYYY hh24:mi:ss')
                 - to_timestamp_tz (d2,'DD MON YYYY hh24:mi:ss') as intrvl
         from dual
         );


select extract(second from intrvl) into ss from (
          select   to_timestamp_tz (d1,'DD MON YYYY hh24:mi:ss')
                 - to_timestamp_tz (d2,'DD MON YYYY hh24:mi:ss') as intrvl
         from dual
         );
        DBMS_OUTPUT.PUT_LINE('DD  HH24  MI SS');
        DBMS_OUTPUT.PUT_LINE(dd || '  '||hh24||'  ' ||mi||'  ' ||ss);
end;
/

declare
begin 
d061(sysdate+1,sysdate);
end;
/

output: 输出:

DD  HH24  MI SS
1  0  0  0

A proposed solution... 拟议的解决方案...

SET DATEFORMAT YMD
DECLARE @aux TABLE (TimeFrom datetime, TimeTo datetime, Status int)
DECLARE @res TABLE (YearMonth varchar(7), Status int, Sc int)

INSERT INTO @aux VALUES ('2018-04-01 09:03:27','2018-07-15 10:11:13',12)

DECLARE @secs int, @dt datetime, @fi datetime, @fo datetime,
        @f1 datetime, @f2 datetime, @st int

SELECT TOP 1 @fi = TimeFrom, @fo = TimeTo, @st = status FROM @aux

SELECT @dt = DATEADD(mm, DATEDIFF(mm, 0, @fi), 0), @f1 = @fi
WHILE @dt < @fo
BEGIN
    SELECT @f2 = CASE 
            WHEN DATEADD(mm, 1, @dt) < @fo
                THEN DATEADD(mm, 1, @dt)
                ELSE DATEADD(ss, 1, @fo)
            END
    SELECT @secs = DATEDIFF(ss, @f1, DATEADD(s, - 1, @f2))
    INSERT INTO @res VALUES (CONVERT(varchar(7), @dt, 120), @st, @secs)
    SELECT @f1 = @f2, @dt = DATEADD(mm, 1, @dt)
END

SELECT YearMonth,status
    ,CAST(Sc / 3600 AS varchar) + '.'
    +RIGHT('0' + CAST((Sc - 3600 * (Sc / 3600)) / 60 AS varchar(2)), 2) + '.'
    +RIGHT('0' + CAST(Sc % 60 AS varchar(2)), 2) AS [Duration (hhh.mm.ss)]
FROM @res

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM