日期之间的一天中的小时数

时间:2018-07-05 09:57:24

标签: sql sql-server

我有一个包含以下列的表格:gkey,ata,atd

ata和atd都是日期时间。

我想每天计算出一些东西在离开之前已经到达的时间。

示例数据:

import requests
import bs4

url = "http://www.example.com/test.html"
r = requests.get(url)

html = r.text
soup = bs4.BeautifulSoup(html, 'html.parser')
tables = soup.findAll('table')[1]

for tr in tables.findAll('tr')[0:3]:
    cols = tr.findAll('td')
    for tds in cols:
        print ('{:5}'.format(str(tds.text)), end="")
    print()

预期结果:

+---------+-------------------------+-------------------------+
| gkey    | ata                     | atd                     |
+---------+-------------------------+-------------------------+
| 1142227 | 14/06/2017 17:30:00.000 | 15/06/2017 05:30:00.000 |
+---------+-------------------------+-------------------------+
| 1322667 | 18/01/2018 16:17:00.000 | 18/01/2018 20:45:00.000 |
+---------+-------------------------+-------------------------+
| 1339468 | 26/02/2018 23:57:00.000 | 27/02/2018 11:35:00.000 |
+---------+-------------------------+-------------------------+

1 个答案:

答案 0 :(得分:3)

您可以使用递归CTE:

with cte as (
      select gkey, ata, atd,
             ata as day_start,
             (case when cast(ata as date) = cast(atd as date)
                   then atd
                   else dateadd(day, 1, cast(ata as date))
              end) as day_end
      from t
      union all
      select gkey, day_end, atd,
             day_end as day_start,
             (case when cast(day_end as date) = cast(atd as date)
                   then day_end
                   else dateadd(day, 1, cast(day_end as date))
              end) as day_end
      from cte
      where day_start < cast(atd as date)
     )
select cte.*,
       datediff(minute, day_start, day_end) / 60.0 as num_hours
from cte;

Here是一个SQL提琴。