【问题标题】:Compare date Range by Month (Integer) and Year (Integer)按月(整数)和年(整数)比较日期范围
【发布时间】:2021-01-16 15:35:59
【问题描述】:

我在比较日期范围时遇到问题。我必须验证某个月份和年份内的日期。月份和年份是整数值。

注意:我正在使用 Oracle 数据库的 OUTSYSTEMS 聚合

查询的两个结果示例:

    Start Date    End Date 
1   2020-08-16    2020-10-14
2   2019-11-01    2020-08-15

案例 1

输入:

Month = 9
Year = 2020

预期结果:

    Start Date    End Date 
1   2020-08-16    2020-10-14

案例 2

输入:

Month = 8
Year = 2020

预期结果:

    Start Date    End Date 
1   2020-08-16    2020-10-14
2   2019-11-01    2020-08-15

案例 3

输入:

Month = 3
Year = 2020

预期结果:

    Start Date    End Date 
2   2019-11-01    2020-08-15

案例 4

输入:

Month = 10
Year = 2019

预期结果:无行

选择采用 Java 方式。我正在使用像 Month() 和 Year() 这样的系统函数将行转换为整数。

这样

((Month(StartDate) <= Month and Month(EndDate) = Month)
and
(Year(StartDate) <= Year and Year(EndDate) = Year))
or
((Month(StartDate) <= Month and Month(EndDate) = Month)
and
(Year(StartDate) <= Year and Year(EndDate) = Year))

上面的代码不起作用。我尝试了许多组合但没有成功。我没有特殊的比较功能。对于我的分析,我有四个场景要创建,以显示我正在研究的月份和年份中包含的日期。但我没有让代码工作。有人可以为我照亮道路

【问题讨论】:

  • 请用您正在运行的数据库标记您的问题:mysql、sql-server、postgresql...?日期函数是高度特定于供应商的
  • 谢谢。我正在使用 Outsystems 聚合。
  • 我不认为 Outsystems Aggregates 是一个数据库(至少基于标签信息)。您需要找出应用程序背后的数据库是什么。
  • 好的。是甲骨文。但我需要汇总使用(所以我指的是sintax bove)。谢谢
  • Oracle 没有 year()month() 函数 - 因此在该数据库中,您现有的代码会引发编译错误。

标签: java sql oracle datetime outsystems


【解决方案1】:

一种简单的方法使用算术:

where year * 100 + month 
    between year(startdate) * 100 + month(startdate)
        and year(enddate)   * 100 + month(enddate)

但是,这可能不是最有效的方法。通常,您希望避免在您过滤的列上应用函数。更好的选择是将年/月参数转换为日期 - 不幸的是,您没有标记数据库,并且日期函数是高度特定于供应商的,因此实际上不可能建议。

如果你不想要between:

where year * 100 + month >= year(startdate) * 100 + month(startdate)
  and year * 100 + month <= year(enddate)   * 100 + month(enddate)

【讨论】:

  • 感谢您的帮助。但不幸的是,它对我不起作用。两个原因。我仅限于我提出的语法(所以我不能使用“介于”或“哪里”)。其次,我无法将其转换为日期,因为我必须设置一天。在这种情况下,这一天是无关紧要的,可能会影响搜索。在我相关的案例 2 中,我希望查询给我两个结果
  • @PedroEstevesAntunes:您可以轻松地将between 转换为两个不等式条件。查看我的更新。
  • 我只能使用上面的语法。只需使用字段并使用基本运算符,如 >= 或
  • @PedroEstevesAntunes:你是什么意思?您可以使用year(startdate),但不能使用year(startdate) * 100 + month(startdate)
  • 抱歉专线小巴。它就像一个魅力。现在我可以按日期过滤。我需要更好地分析你的过滤器,看看我是否理解。我觉得很奇怪。但它完美地工作。疯狂的东西:)
【解决方案2】:

这行得通吗?考虑您输入的月份 m 和年份 y

StartDate <= AddDays(AddMonths(NewDate(Year(y), Month(m), 1),1)-1)
and
EndDate >= NewDate(Year(y), Month(m), 1))

思路是这样的:过滤所有小于输入月份最后一天的开始日期和所有大于输入月份第一天的结束日期。

关于性能,使用这种方法,您不必对要过滤的列执行任何逻辑/过滤。

【讨论】:

  • 你好@pipo。GMB 解决方案运行良好。按天过滤的问题我不知道它是否会起作用。因为我可以有两个或多个日期在同一个月完成和开始。我认为在这里按天过滤会破坏这项研究。
  • 我相信这个解决方案涵盖了这种情况。但没有什么比一个简单的测试来检查它更好的了
【解决方案3】:

独立于供应商的解决方案

GMB 的回答很好,如果是我,我可能会同意。正如 GMB 所说,它是特定于供应商的,因为日期函数是。如果您想要一个适用于数据库供应商的解决方案,请使用 Java 进行日期数学运算,这样您只需要在数据库中进行简单的日期比较。

    int month = 8;
    int year = 2020;

    YearMonth ym = YearMonth.of(year, month);
    LocalDate monthStart = ym.atDay(1);
    LocalDate monthEnd = ym.atEndOfMonth();

当您将这些日期传递给您的查询时,您的搜索条件可能会很简单:

  where startDate <= monthEnd and endDate >= monthStart

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 2017-07-15
    相关资源
    最近更新 更多