【问题标题】:Oracle SQL query about Date关于日期的 Oracle SQL 查询
【发布时间】:2017-03-07 01:42:44
【问题描述】:

我有一个名为 availableTimeslot 的数据库表,其中包含字段 pkstartDateendDate,例如

PK   startDate             endDate
1.   2017-03-07 09:00:00   2017-03-07 18:00:00
2.   2017-03-07 18:00:00   2017-03-07 21:00:00
3.   2017-03-08 09:00:00   2017-03-08 18:00:00

从 09:00:00 到 18:00:00 的记录表示是上午时段,而 18:00:00 到 23:00:00 表示是下午时段

存储可供客户选择的可用时间段日期(例如 2017-03-06、2017-03-08)。

我可以使用一个查询来获取从订单日期后的第二天开始的正好 10 个可用时间段日期吗?

例如如果我在 2016-03-07 订购产品,则查询返回

2017-03-08 09:00:00
2017-03-08 18:00:00 
2017-03-09 09:00:00
2017-03-09 18:00:00
2017-03-10 ...
2017-03-11 ...
2017-03-13 ... 

因为 12 是公共假期,不在表中。

简而言之,它返回 10 个日期(5 天,每天有上午和下午会话)

备注:可用时隙日期按顺序排列,但可能不连续

【问题讨论】:

  • 编辑您的问题并提供示例数据和所需的结果。

标签: sql oracle


【解决方案1】:
select available_date
from   ( select available_date, row_number() over (order by available_date) as rn 
         from   your_table
         where  available_date > :order_date
       )
where  rn <= 5;

:order_date 是一个绑定变量——用户/客户通过界面输入的日期。

【讨论】:

    【解决方案2】:

    您希望为单个客户提供 5 个吗?

    select ts.*
    from (select ts.*
          from customer c join
               timeslots ts
               on ts.date > c.orderdate
          where c.customerid = v_customerid
          order by ts.date asc
         ) ts
    where rownum <= 5
    

    【讨论】:

    • 感谢您的回答,但我的查询只需要满足一张桌子
    猜你喜欢
    • 1970-01-01
    • 2021-02-17
    • 2021-10-21
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多