AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 163467
Accepted
Luciano Andress Martini
Luciano Andress Martini
Asked: 2017-02-08 05:58:44 +0800 CST2017-02-08 05:58:44 +0800 CST 2017-02-08 05:58:44 +0800 CST

根据开始停止列从表行中获取日期间隔

  • 772

我有一个返回以下结果集的表:

  mydate    | startstop
------------+----------
 2018-02-07 | start
 2018-02-14 | stop
 2017-02-06 | start
 2017-02-12 | stop
 2016-02-05 | start 
 2016-02-12 | stop 

我需要知道我的当前日期是否在其中一个时间间隔内,例如,如果我从当前日期查询表'2017-02-07',我需要获取'TRUE'.

我知道它看起来很简单,但它并不简单!

我发现的最好的是:

       select true 
        where '2017-02-06'>=
         (select mydate from mytable where starstop='start' order by id limit 1) 
        and '2017-02-06' <= 
         (select mydate from mytable where startstop='stop' order by id limit 1);

如果日期在每个间隔之一中,它会返回TRUE,但前提是表格在未来没有间隔,正如您所看到的,我的表格在未来有间隔。

注:数据库管理系统为PostgreSQL 9.1

postgresql postgresql-9.1
  • 2 2 个回答
  • 114 Views

2 个回答

  • Voted
  1. Best Answer
    ypercubeᵀᴹ
    2017-02-08T06:41:12+08:002017-02-08T06:41:12+08:00

    其实很简单。从想要的日期开始,按日期向下排列,直到找到第一行。如果是,'start'那么你就在一个区间内。如果是'stop',则您不是:

    select 
      ( select startstop
        from mytable 
        where mydate = '2017-02-07' and startstop = 'start'
           or mydate < '2017-02-07'
        order by mydate desc
        limit 1
      ) = 'start'
      as result ;
    

    复杂WHERE的是处理包含的日期范围。如果使用 inclusive-exclusive,条件会更简单。

    类似的方式——也许更清楚一点——是:

    with ct as 
      ( select mydate, startstop
        from mytable 
        where mydate <= '2017-02-07'
        order by mydate desc
        limit 1
      )
    select 
           ('start') = (select startstop from ct) 
        or ('2017-02-07', 'stop') = (table ct) 
      as result ;
    
    • 3
  2. CaM
    2017-02-08T06:12:33+08:002017-02-08T06:12:33+08:00

    所以看起来您需要将其转换为如下所示的内容:

    startdate   |   stopdate
    2016-02-06  |   2016-02-12
    2017-02-06  |   2017-02-12
    

    在那之后,你可以做一个看起来像的查询

    select startdate, stopdate
    from mytransform
    where startdate <= '2017-02-06'
    and stopdate >= '2017-02-06'
    

    这可能需要某种游标来解析并匹配开始和停止日期以建立您的间隔。

    • 0

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve