Informix - 两个日期之间

Posted

技术标签:

【中文标题】Informix - 两个日期之间【英文标题】:Informix - BETWEEN two dates 【发布时间】:2016-11-28 03:16:32 【问题描述】:

我的查询类似于...

SELECT * FROM table WHERE datetime BETWEEN two_sundays_ago AND last_sunday

我查看了 IBM (link) 的日期/时间函数以及其他一些文档,但我很难理解如何做两件事。

    首先是如何调整时区差异(从小时值中减去 6),同时仍保留日期时间值的其余部分。日期时间类型为 DATETIME YEAR TO FRACTION (3)。

    其次是如何获取两个日期值,two_sundays_ago 和 last_sunday。例如,如果今天是 2016 年 11 月 28 日星期一,我希望查询选择 16 年 11 月 20 日到 16 年 11 月 27 日之间的值。

我在 BIDS 中使用表达式 =DateAdd("h",6,DateAdd("d",-Weekday(Now())-6,Today)) for two_sundays_ago 执行此操作,但在此查询中我正在汇总值,因此我的理解是我无法选择稍后过滤的日期时间值。

【问题讨论】:

(1)“调整时区差异”是什么意思?哪个时区?系统如何知道哪个时区?简短的回答是添加或减去一个适当的间隔:±INTERVAL(4:30) HOUR TO MINUTE,增量为 4 小时 30 分钟。 (2) 11 月 27 日星期日,相关日期是 11 月 13 日星期日和 11 月 20 日星期日? 11 月 28 日星期一,相关日期是 11 月 20 日星期日和 11 月 27 日星期日? 11月26日星期六,相关日期也是13日和20日? (3) 不清楚你在尝试什么;请显示一些 SQL 作为摘要内容。 乔恩,正确,你说的几乎所有内容都是正确的。我不确定您是否熟悉 BIDS,但我基本上是在尝试为 Informix 编写 =DateAdd(etc. see post) 等效项。我只是想根据今天的日期从上周获取信息,并根据过程中的时区差异进行调整 SELECT * FROM table WHERE datetime BETWEEN (today-weekday-7) AND (today-weekday) .... datetime 类型是 DATETIME YEAR TO FRACTION (3)。然后需要调整时区。数据库是 UTC,我们是中部时间 当您询问 11 月 13 日至 20 日之间的日期时,您想要全部 8 天吗?从 2016-11-13 00:00:00 到 2016-11-20 23:59:59.99999?或者如果当前时间是 10:02:39,你想要从 2016-11-13 10:02:39 到 2016-11-20 10:02:39?如果您想要“一天的开始和结束”,您是否希望它们处于时区调整值中(美国/中部的午夜到午夜) - 或者 UTC 的午夜,或其他什么? 从 2016-11-13 00:00:00 到 2016-11-19 中部时间 23:59:59.999(调整后) 【参考方案1】:

我认为您可以从实现MOD 变体的函数中受益,其中variant_MOD(i, j) 返回1..j 范围内的值而不是0..j-1 — 当来自MOD(i, j) 的值将是@987654326 @,而是返回 j。忽略负值,这个 jmod() 完成了这项工作:

CREATE FUNCTION jmod(n INTEGER, m INTEGER) RETURNING INTEGER AS v;
    LET n = MOD(n, m);
    IF (n = 0) THEN LET n = m; END IF;
    RETURN n;
END FUNCTION;

使用此功能,以及一个“操作”表t,其中包含以 UTC 记录的日期/时间值:

CREATE TABLE t (dt DATETIME YEAR TO FRACTION(3));

还有一个单独的表 ref_date,其中包含一个用于参考日期的 ref_date 列,以及一个包含例如今天日期的单行:

CREATE TABLE ref_date (ref_date DATE NOT NULL UNIQUE);
INSERT INTO ref_date VALUES(TODAY);

然后您可以运行这样的查询以选择美国/中部时间(目前为 UTC-06:00)的相关行:

SELECT dt_utc,
       dt_central,
       rd.ref_date - jmod(WEEKDAY(rd.ref_date), 7) - 7     AS two_sunday_ago,
       rd.ref_date - jmod(WEEKDAY(rd.ref_date), 7)         AS one_sunday_ago
  FROM (SELECT dt AS dt_utc, dt - INTERVAL(6:00) HOUR TO MINUTE AS dt_central FROM t) AS mapped_time
  JOIN ref_date AS rd ON 1 = 1
 WHERE dt_central >= (rd.ref_date - jmod(WEEKDAY(rd.ref_date), 7) - 7)
   AND dt_central <  (rd.ref_date - jmod(WEEKDAY(rd.ref_date), 7))
 ORDER BY dt_central
;

这将创建具有ON 1 = 1 条件的两个表的笛卡尔积。在 Informix 12.10 中,您可以改用 CROSS JOIN:

SELECT dt_utc,
       dt_central,
       rd.ref_date - jmod(WEEKDAY(rd.ref_date), 7) - 7     AS two_sunday_ago,
       rd.ref_date - jmod(WEEKDAY(rd.ref_date), 7)         AS one_sunday_ago
  FROM (SELECT dt AS dt_utc, dt - INTERVAL(6:00) HOUR TO MINUTE AS dt_central FROM t) AS mapped_time
 CROSS JOIN ref_date AS rd
 WHERE dt_central >= (rd.ref_date - jmod(WEEKDAY(rd.ref_date), 7) - 7)
   AND dt_central <  (rd.ref_date - jmod(WEEKDAY(rd.ref_date), 7))
 ORDER BY dt_central
;

我不确定何时添加了 CROSS JOIN — 试试吧;它可能对你有用。

给定一些部分随机、部分系统的日期,例如:

2016-11-01 06:07:02.029
2016-11-01 11:19:47.027
2016-11-01 14:08:31.677
2016-11-01 16:40:23.650
2016-11-01 17:47:37.206
2016-11-01 17:55:31.371
2016-11-02 10:42:05.401
2016-11-02 10:48:21.017
2016-11-02 18:27:41.661
2016-11-02 23:09:18.752
2016-11-02 23:50:20.358
2016-11-03 03:36:08.826
2016-11-03 06:07:01.217
2016-11-03 14:19:57.977
2016-11-03 16:03:01.813
2016-11-04 00:24:43.164
2016-11-04 00:58:09.912
2016-11-04 07:37:17.457
2016-11-04 07:52:10.984
2016-11-04 11:36:59.265
2016-11-04 16:44:17.827
2016-11-05 13:35:50.577
2016-11-05 15:12:14.804
2016-11-05 15:30:10.579
2016-11-05 15:45:05.364
2016-11-05 19:15:30.116
2016-11-05 22:25:58.315
2016-11-06 05:25:46.535
2016-11-06 05:32:00.865
2016-11-06 10:35:07.787
2016-11-06 20:28:40.664
2016-11-06 20:57:01.862
2016-11-07 01:31:02.657
2016-11-07 03:17:15.625
2016-11-07 06:54:13.873
2016-11-07 07:19:33.032
2016-11-07 09:12:31.531
2016-11-07 12:53:07.034
2016-11-07 17:14:34.281
2016-11-08 03:53:15.886
2016-11-08 06:20:39.009
2016-11-08 10:30:20.182
2016-11-08 14:25:14.173
2016-11-08 16:34:39.781
2016-11-08 17:59:40.327
2016-11-09 01:45:40.142
2016-11-09 06:12:05.475
2016-11-09 18:10:02.483
2016-11-09 18:29:01.374
2016-11-10 03:15:42.938
2016-11-10 14:56:39.354
2016-11-10 15:19:28.320
2016-11-10 17:05:13.327
2016-11-10 19:21:54.386
2016-11-10 21:46:10.826
2016-11-11 05:36:52.942
2016-11-11 08:35:54.840
2016-11-11 12:38:57.564
2016-11-11 18:05:08.286
2016-11-11 18:11:33.612
2016-11-11 23:22:55.222
2016-11-12 01:40:20.976
2016-11-12 07:42:37.907
2016-11-12 12:12:51.797
2016-11-12 12:34:56.000
2016-11-12 23:11:16.077
2016-11-13 02:37:34.081
2016-11-13 05:35:05.835
2016-11-13 05:54:12.853
2016-11-13 07:31:33.380
2016-11-13 11:34:56.000
2016-11-13 15:31:13.543
2016-11-13 20:20:31.259
2016-11-13 21:52:01.573
2016-11-14 02:00:39.716
2016-11-14 02:42:48.699
2016-11-14 04:31:23.312
2016-11-14 09:24:15.559
2016-11-14 10:34:56.000
2016-11-14 12:45:36.275
2016-11-14 22:55:42.745
2016-11-15 01:47:49.193
2016-11-15 13:31:26.524
2016-11-15 15:30:24.267
2016-11-15 19:07:17.032
2016-11-15 19:09:01.029
2016-11-15 19:34:56.000
2016-11-15 20:14:28.359
2016-11-16 00:01:43.336
2016-11-16 08:53:21.018
2016-11-16 09:06:47.542
2016-11-16 18:34:56.000
2016-11-16 19:45:41.016
2016-11-16 21:57:11.321
2016-11-16 23:18:24.716
2016-11-17 03:33:05.719
2016-11-17 07:01:09.417
2016-11-17 08:08:16.121
2016-11-17 09:41:19.698
2016-11-17 10:42:33.239
2016-11-17 12:15:27.553
2016-11-17 17:34:56.000
2016-11-17 21:53:55.909
2016-11-18 04:24:15.725
2016-11-18 04:46:03.008
2016-11-18 05:55:30.590
2016-11-18 12:14:24.298
2016-11-18 12:58:37.578
2016-11-18 15:49:14.780
2016-11-18 16:34:56.000
2016-11-19 05:02:52.783
2016-11-19 07:30:51.461
2016-11-19 09:56:43.041
2016-11-19 15:34:56.000
2016-11-19 18:16:47.583
2016-11-19 18:50:38.236
2016-11-20 02:38:04.760
2016-11-20 03:36:04.681
2016-11-20 05:59:59.999
2016-11-20 06:00:00.000
2016-11-20 09:14:10.393
2016-11-20 09:43:03.942
2016-11-20 11:49:54.133
2016-11-20 14:34:56.000
2016-11-20 16:26:38.713
2016-11-21 03:35:07.572
2016-11-21 13:34:56.000
2016-11-21 20:37:13.670
2016-11-21 21:50:15.654
2016-11-21 23:45:38.274
2016-11-22 05:39:06.759
2016-11-22 07:41:51.497
2016-11-22 09:15:53.879
2016-11-22 12:34:56.000
2016-11-22 15:23:12.384
2016-11-22 20:22:38.939
2016-11-22 21:50:18.608
2016-11-22 22:44:44.659
2016-11-23 03:35:36.576
2016-11-23 10:25:07.755
2016-11-23 11:34:56.000
2016-11-23 13:18:18.491
2016-11-23 18:06:22.202
2016-11-24 01:15:31.356
2016-11-24 02:28:40.031
2016-11-24 10:34:56.000
2016-11-24 12:17:26.300
2016-11-24 15:39:32.904
2016-11-24 16:50:10.130
2016-11-24 22:41:28.450
2016-11-24 22:58:19.290
2016-11-25 01:43:38.514
2016-11-25 05:55:49.725
2016-11-25 19:34:56.000
2016-11-25 21:23:51.303
2016-11-25 23:15:01.760
2016-11-26 01:30:18.578
2016-11-26 09:04:10.808
2016-11-26 09:40:47.942
2016-11-26 14:51:15.283
2016-11-26 17:45:58.383
2016-11-26 18:34:56.000
2016-11-27 00:00:00.000
2016-11-27 05:59:59.999
2016-11-27 06:00:00.000
2016-11-27 07:34:30.816
2016-11-27 08:38:15.413
2016-11-27 16:05:01.385
2016-11-27 17:34:56.000
2016-11-28 00:42:58.846
2016-11-28 05:12:39.664
2016-11-28 09:38:25.615
2016-11-28 12:47:44.086
2016-11-28 15:32:46.675
2016-11-28 16:34:56.000
2016-11-28 20:02:04.530
2016-11-28 20:06:25.688
2016-11-29 07:37:28.761
2016-11-29 14:29:18.228
2016-11-29 15:34:56.000
2016-11-29 17:54:28.873
2016-11-29 17:54:36.695
2016-11-29 20:31:31.838
2016-11-30 04:27:36.828
2016-11-30 09:08:41.064
2016-11-30 11:30:09.853
2016-11-30 12:40:14.306
2016-11-30 14:34:56.000
2016-11-30 18:55:15.446
2016-11-30 19:53:16.446
2016-11-30 19:53:56.432

并将参考日期设置为2016-11-28,查询的输出为:

dt_utc|dt_central|two_sunday_ago|one_sunday_ago
DATETIME YEAR TO FRACTION(3)|DATETIME YEAR TO FRACTION(3)|DATE|DATE
2016-11-20 06:00:00.000|2016-11-20 00:00:00.000|2016-11-20|2016-11-27
2016-11-20 09:14:10.393|2016-11-20 03:14:10.393|2016-11-20|2016-11-27
2016-11-20 09:43:03.942|2016-11-20 03:43:03.942|2016-11-20|2016-11-27
2016-11-20 11:49:54.133|2016-11-20 05:49:54.133|2016-11-20|2016-11-27
2016-11-20 14:34:56.000|2016-11-20 08:34:56.000|2016-11-20|2016-11-27
2016-11-20 16:26:38.713|2016-11-20 10:26:38.713|2016-11-20|2016-11-27
2016-11-21 03:35:07.572|2016-11-20 21:35:07.572|2016-11-20|2016-11-27
2016-11-21 13:34:56.000|2016-11-21 07:34:56.000|2016-11-20|2016-11-27
2016-11-21 20:37:13.670|2016-11-21 14:37:13.670|2016-11-20|2016-11-27
2016-11-21 21:50:15.654|2016-11-21 15:50:15.654|2016-11-20|2016-11-27
2016-11-21 23:45:38.274|2016-11-21 17:45:38.274|2016-11-20|2016-11-27
2016-11-22 05:39:06.759|2016-11-21 23:39:06.759|2016-11-20|2016-11-27
2016-11-22 07:41:51.497|2016-11-22 01:41:51.497|2016-11-20|2016-11-27
2016-11-22 09:15:53.879|2016-11-22 03:15:53.879|2016-11-20|2016-11-27
2016-11-22 12:34:56.000|2016-11-22 06:34:56.000|2016-11-20|2016-11-27
2016-11-22 15:23:12.384|2016-11-22 09:23:12.384|2016-11-20|2016-11-27
2016-11-22 20:22:38.939|2016-11-22 14:22:38.939|2016-11-20|2016-11-27
2016-11-22 21:50:18.608|2016-11-22 15:50:18.608|2016-11-20|2016-11-27
2016-11-22 22:44:44.659|2016-11-22 16:44:44.659|2016-11-20|2016-11-27
2016-11-23 03:35:36.576|2016-11-22 21:35:36.576|2016-11-20|2016-11-27
2016-11-23 10:25:07.755|2016-11-23 04:25:07.755|2016-11-20|2016-11-27
2016-11-23 11:34:56.000|2016-11-23 05:34:56.000|2016-11-20|2016-11-27
2016-11-23 13:18:18.491|2016-11-23 07:18:18.491|2016-11-20|2016-11-27
2016-11-23 18:06:22.202|2016-11-23 12:06:22.202|2016-11-20|2016-11-27
2016-11-24 01:15:31.356|2016-11-23 19:15:31.356|2016-11-20|2016-11-27
2016-11-24 02:28:40.031|2016-11-23 20:28:40.031|2016-11-20|2016-11-27
2016-11-24 10:34:56.000|2016-11-24 04:34:56.000|2016-11-20|2016-11-27
2016-11-24 12:17:26.300|2016-11-24 06:17:26.300|2016-11-20|2016-11-27
2016-11-24 15:39:32.904|2016-11-24 09:39:32.904|2016-11-20|2016-11-27
2016-11-24 16:50:10.130|2016-11-24 10:50:10.130|2016-11-20|2016-11-27
2016-11-24 22:41:28.450|2016-11-24 16:41:28.450|2016-11-20|2016-11-27
2016-11-24 22:58:19.290|2016-11-24 16:58:19.290|2016-11-20|2016-11-27
2016-11-25 01:43:38.514|2016-11-24 19:43:38.514|2016-11-20|2016-11-27
2016-11-25 05:55:49.725|2016-11-24 23:55:49.725|2016-11-20|2016-11-27
2016-11-25 19:34:56.000|2016-11-25 13:34:56.000|2016-11-20|2016-11-27
2016-11-25 21:23:51.303|2016-11-25 15:23:51.303|2016-11-20|2016-11-27
2016-11-25 23:15:01.760|2016-11-25 17:15:01.760|2016-11-20|2016-11-27
2016-11-26 01:30:18.578|2016-11-25 19:30:18.578|2016-11-20|2016-11-27
2016-11-26 09:04:10.808|2016-11-26 03:04:10.808|2016-11-20|2016-11-27
2016-11-26 09:40:47.942|2016-11-26 03:40:47.942|2016-11-20|2016-11-27
2016-11-26 14:51:15.283|2016-11-26 08:51:15.283|2016-11-20|2016-11-27
2016-11-26 17:45:58.383|2016-11-26 11:45:58.383|2016-11-20|2016-11-27
2016-11-26 18:34:56.000|2016-11-26 12:34:56.000|2016-11-20|2016-11-27
2016-11-27 00:00:00.000|2016-11-26 18:00:00.000|2016-11-20|2016-11-27
2016-11-27 05:59:59.999|2016-11-26 23:59:59.999|2016-11-20|2016-11-27

您可以在数据中看到精确的截断;时间 05:59:59.99906:00:00.000 出现在正确的位置(并且这些行不是随机出现的)。

给定参考日期 2016-11-27,输出为:

dt_utc|dt_central|two_sunday_ago|one_sunday_ago
DATETIME YEAR TO FRACTION(3)|DATETIME YEAR TO FRACTION(3)|DATE|DATE
2016-11-13 07:31:33.380|2016-11-13 01:31:33.380|2016-11-13|2016-11-20
2016-11-13 11:34:56.000|2016-11-13 05:34:56.000|2016-11-13|2016-11-20
2016-11-13 15:31:13.543|2016-11-13 09:31:13.543|2016-11-13|2016-11-20
2016-11-13 20:20:31.259|2016-11-13 14:20:31.259|2016-11-13|2016-11-20
2016-11-13 21:52:01.573|2016-11-13 15:52:01.573|2016-11-13|2016-11-20
2016-11-14 02:00:39.716|2016-11-13 20:00:39.716|2016-11-13|2016-11-20
2016-11-14 02:42:48.699|2016-11-13 20:42:48.699|2016-11-13|2016-11-20
2016-11-14 04:31:23.312|2016-11-13 22:31:23.312|2016-11-13|2016-11-20
2016-11-14 09:24:15.559|2016-11-14 03:24:15.559|2016-11-13|2016-11-20
2016-11-14 10:34:56.000|2016-11-14 04:34:56.000|2016-11-13|2016-11-20
2016-11-14 12:45:36.275|2016-11-14 06:45:36.275|2016-11-13|2016-11-20
2016-11-14 22:55:42.745|2016-11-14 16:55:42.745|2016-11-13|2016-11-20
2016-11-15 01:47:49.193|2016-11-14 19:47:49.193|2016-11-13|2016-11-20
2016-11-15 13:31:26.524|2016-11-15 07:31:26.524|2016-11-13|2016-11-20
2016-11-15 15:30:24.267|2016-11-15 09:30:24.267|2016-11-13|2016-11-20
2016-11-15 19:07:17.032|2016-11-15 13:07:17.032|2016-11-13|2016-11-20
2016-11-15 19:09:01.029|2016-11-15 13:09:01.029|2016-11-13|2016-11-20
2016-11-15 19:34:56.000|2016-11-15 13:34:56.000|2016-11-13|2016-11-20
2016-11-15 20:14:28.359|2016-11-15 14:14:28.359|2016-11-13|2016-11-20
2016-11-16 00:01:43.336|2016-11-15 18:01:43.336|2016-11-13|2016-11-20
2016-11-16 08:53:21.018|2016-11-16 02:53:21.018|2016-11-13|2016-11-20
2016-11-16 09:06:47.542|2016-11-16 03:06:47.542|2016-11-13|2016-11-20
2016-11-16 18:34:56.000|2016-11-16 12:34:56.000|2016-11-13|2016-11-20
2016-11-16 19:45:41.016|2016-11-16 13:45:41.016|2016-11-13|2016-11-20
2016-11-16 21:57:11.321|2016-11-16 15:57:11.321|2016-11-13|2016-11-20
2016-11-16 23:18:24.716|2016-11-16 17:18:24.716|2016-11-13|2016-11-20
2016-11-17 03:33:05.719|2016-11-16 21:33:05.719|2016-11-13|2016-11-20
2016-11-17 07:01:09.417|2016-11-17 01:01:09.417|2016-11-13|2016-11-20
2016-11-17 08:08:16.121|2016-11-17 02:08:16.121|2016-11-13|2016-11-20
2016-11-17 09:41:19.698|2016-11-17 03:41:19.698|2016-11-13|2016-11-20
2016-11-17 10:42:33.239|2016-11-17 04:42:33.239|2016-11-13|2016-11-20
2016-11-17 12:15:27.553|2016-11-17 06:15:27.553|2016-11-13|2016-11-20
2016-11-17 17:34:56.000|2016-11-17 11:34:56.000|2016-11-13|2016-11-20
2016-11-17 21:53:55.909|2016-11-17 15:53:55.909|2016-11-13|2016-11-20
2016-11-18 04:24:15.725|2016-11-17 22:24:15.725|2016-11-13|2016-11-20
2016-11-18 04:46:03.008|2016-11-17 22:46:03.008|2016-11-13|2016-11-20
2016-11-18 05:55:30.590|2016-11-17 23:55:30.590|2016-11-13|2016-11-20
2016-11-18 12:14:24.298|2016-11-18 06:14:24.298|2016-11-13|2016-11-20
2016-11-18 12:58:37.578|2016-11-18 06:58:37.578|2016-11-13|2016-11-20
2016-11-18 15:49:14.780|2016-11-18 09:49:14.780|2016-11-13|2016-11-20
2016-11-18 16:34:56.000|2016-11-18 10:34:56.000|2016-11-13|2016-11-20
2016-11-19 05:02:52.783|2016-11-18 23:02:52.783|2016-11-13|2016-11-20
2016-11-19 07:30:51.461|2016-11-19 01:30:51.461|2016-11-13|2016-11-20
2016-11-19 09:56:43.041|2016-11-19 03:56:43.041|2016-11-13|2016-11-20
2016-11-19 15:34:56.000|2016-11-19 09:34:56.000|2016-11-13|2016-11-20
2016-11-19 18:16:47.583|2016-11-19 12:16:47.583|2016-11-13|2016-11-20
2016-11-19 18:50:38.236|2016-11-19 12:50:38.236|2016-11-13|2016-11-20
2016-11-20 02:38:04.760|2016-11-19 20:38:04.760|2016-11-13|2016-11-20
2016-11-20 03:36:04.681|2016-11-19 21:36:04.681|2016-11-13|2016-11-20
2016-11-20 05:59:59.999|2016-11-19 23:59:59.999|2016-11-13|2016-11-20

【讨论】:

感谢您对这个乔纳森的帮助。不幸的是,我认为我们不能碰数据库,它是用于思科呼叫中心的,不想惹它。我将尝试在运行查询后汇总值。不过,我在阅读您的脚本时学到了很多东西,我们可以尝试类似的东西而不创建新表。再次感谢您! ref_date 表可能是临时表而不是永久表。您还可以编写一个粗略的存储过程,传递参考日期并返回正确的数据。有无穷无尽的方法来处理这些东西。关键项目有:(1)子查询创建Central时间戳; (2) jmod 函数(名称完全由您选择)正确计算日期偏移量。如果函数被认为有问题,您可能可以写一个 case 表达式来代替:CASE MOD(i, j) WHEN 0 THEN j ELSE MOD(i, j) END,但它不够优雅,并且使代码更难理解。

以上是关于Informix - 两个日期之间的主要内容,如果未能解决你的问题,请参考以下文章

两个日期时间之间的差异(以毫秒为单位)(Informix)

Informix 日期时间选择标准(在运算符之间)

Informix:两个相似表之间的区别?

Informix:日期时间操作

Informix:.NET 驱动程序:默认日期时间格式

无法根据 Informix 中的日期过滤器过滤记录