db2数据库 我要根据当前时间减去一个小时作为开始时间,当前时间作为结束时间查询总数量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了db2数据库 我要根据当前时间减去一个小时作为开始时间,当前时间作为结束时间查询总数量相关的知识,希望对你有一定的参考价值。

select count(*) fromtable where req_time between (当前时间减去一个小时) and 当前时间;
当前时间怎么获得,还有(当前时间减去一个小时)这个这么写?

参考技术A select count(*) fromtable where req_time between current time - 1 hours and current time追问

我的时间类型是:timestamp
select count(*) from ii_mms_snd where req_time between current timestamp(这个地方怎么减一个小时) and current timestamp;

追答

select count(*) from ii_mms_snd where req_time between current timestamp - 1000*60*60 microseconds and current timestamp;

本回答被提问者采纳

如何从 Pandas 数据框列中的日期时间减去 3 小时?

【中文标题】如何从 Pandas 数据框列中的日期时间减去 3 小时?【英文标题】:How can I subtract 3 hours from a datetime in a Pandas dataframe column? 【发布时间】:2019-04-03 13:00:38 【问题描述】:

我在 gmt 中有两列日期时间,我需要从这个日期时间中减去三个小时。例如在第 4 行中,我需要在 3 小时内减去 startdate,结果是:08/02/2018 17:20:0。在同一行 4 中,我需要在 3 小时内减去 enddate,结果是:08/02/2018 21:50:0

初始表:

cpf  day  startdate              enddate
1234  1   08/01/2018 12:50:0     08/01/2018 15:30:0
1234  1   08/01/2018 14:30:0     08/01/2018 15:40:0
1234  1   08/01/2018 14:50:0     08/01/2018 15:50:0
1234  2   08/02/2018 20:20:0     08/03/2018 00:50:0
1234  3   08/03/2018 01:00:0     08/03/2018 03:50:0
1235  1   08/01/2018 11:50:0     08/01/2018 15:20:0
5212  1   08/01/2018 14:50:0     08/01/2018 15:20:0

结果表:

cpf  day  startdate              enddate
1234  1   08/01/2018 09:50:0     08/01/2018 10:30:0
1234  1   08/01/2018 11:30:0     08/01/2018 10:40:0
1234  1   08/01/2018 11:50:0     08/01/2018 10:50:0
1234  2   08/02/2018 17:20:0     08/02/2018 21:50:0
1234  3   08/02/2018 22:00:0     08/03/2018 00:50:0
1235  1   08/01/2018 08:50:0     08/01/2018 10:20:0
5212  1   08/01/2018 11:50:0     08/01/2018 10:20:0

如何在 Python 中做到这一点?

【问题讨论】:

熊猫有一个时间增量。 pandas.pydata.org/pandas-docs/stable/timedeltas.html Subtract hours and minutes from time的可能重复 【参考方案1】:

您可以使用timedelta

from datetime import timedelta

df['startdate'] = pd.to_datetime(df['startdate']) - timedelta(hours=3)
df['enddate'] = pd.to_datetime(df['enddate']) - timedelta(hours=3)

【讨论】:

【参考方案2】:

我相信您需要转换列 to_datetime 并减去 3 hours timedelta:

cols = ['startdate','enddate']
td = pd.Timedelta(3, unit='h')
df[cols] = df[cols].apply(lambda x: pd.to_datetime(x, format='%d/%m/%Y %H:%M:%S') - td

如果想分别为每一列应用解决方案:

td = pd.Timedelta(3, unit='h')
df['startdate'] = pd.to_datetime(df['startdate'], format='%d/%m/%Y %H:%M:%S') - td
df['enddate'] = pd.to_datetime(df['enddate'], format='%d/%m/%Y %H:%M:%S') - td

print (df)
    cpf  day           startdate             enddate
0  1234    1 2018-01-08 09:50:00 2018-01-08 12:30:00
1  1234    1 2018-01-08 11:30:00 2018-01-08 12:40:00
2  1234    1 2018-01-08 11:50:00 2018-01-08 12:50:00
3  1234    2 2018-02-08 17:20:00 2018-03-07 21:50:00
4  1234    3 2018-03-07 22:00:00 2018-03-08 00:50:00
5  1235    1 2018-01-08 08:50:00 2018-01-08 12:20:00
6  5212    1 2018-01-08 11:50:00 2018-01-08 12:20:00

如果需要,最后将日期时间转换为自定义格式:

df['startdate'] = df['startdate'].dt.strftime('%d/%m/%Y %H:%M:%S')
df['enddate'] = df['enddate'].dt.strftime('%d/%m/%Y %H:%M:%S')
print (df)
    cpf  day            startdate              enddate
0  1234    1  08/01/2018 09:50:00  08/01/2018 12:30:00
1  1234    1  08/01/2018 11:30:00  08/01/2018 12:40:00
2  1234    1  08/01/2018 11:50:00  08/01/2018 12:50:00
3  1234    2  08/02/2018 17:20:00  07/03/2018 21:50:00
4  1234    3  07/03/2018 22:00:00  08/03/2018 00:50:00
5  1235    1  08/01/2018 08:50:00  08/01/2018 12:20:00
6  5212    1  08/01/2018 11:50:00  08/01/2018 12:20:00

【讨论】:

【参考方案3】:

你可以使用timedelta

示例代码

from datetime import timedelta 
delta = timedelta(hours=-3)

【讨论】:

以上是关于db2数据库 我要根据当前时间减去一个小时作为开始时间,当前时间作为结束时间查询总数量的主要内容,如果未能解决你的问题,请参考以下文章

如何用DELPHI当前日期减五十天

要等到下一个小时开始几秒钟吗?

linux中shell脚本在获取当前系统时间减去一个小时怎么写

由于数据类型错误,无法从时间数据中减去小时数

PHP,需要从 DateTime 中减去 12 小时 30 分钟

Java 根据出生日期获得年龄