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 / 问题 / 149930
Accepted
DecoderReloaded
DecoderReloaded
Asked: 2016-09-17 22:37:38 +0800 CST2016-09-17 22:37:38 +0800 CST 2016-09-17 22:37:38 +0800 CST

如何从 status_track 表中获取状态 start_date 和 end_date?

  • 772

我有两张桌子:

  1. things:

    +------+----------------+------------+
    | id   | current_status | created_at |
    +------+----------------+------------+
    | 7770 | active         | 2016-08-09 |
    +------+----------------+------------+
    
  2. thing_status_tract:

    +----------+-------------+-----------+---------------------+
    | thing_id | status_from | status_to | changed_at          |
    +----------+-------------+-----------+---------------------+
    |     7770 | incomplete  | inactive  | 2016-08-09 16:26:22 |
    |     7770 | inactive    | active    | 2016-08-10 12:31:04 |
    +----------+-------------+-----------+---------------------+
    

我需要以下形式的数据。此表具有特定状态及其相应的开始和结束时间戳thing_id:

+----------+-------------+---------------------+---------------------+
| thing_id | status      | status_start_date   | status_end_date     |
+----------+-------------+---------------------+---------------------+
|     7770 | incomplete  | 2016-08-09 00:00:00 | 2016-08-09 16:26:22 |
|     7770 | inactive    | 2016-08-09 16:26:22 | 2016-08-10 12:31:04 |
|     7770 | active      | 2016-08-10 12:31:04 | now()               |
+----------+-------------+---------------------+---------------------+

如何使用 SQL 查询来做到这一点?

mysql oracle
  • 2 2 个回答
  • 175 Views

2 个回答

  • Voted
  1. Best Answer
    EdStevens
    2016-09-19T14:59:29+08:002016-09-19T14:59:29+08:00

    当然,我之前的回答只是为了回答你眼前的问题。正如我在评论中提到的,您的数据模型本身存在缺陷。你不仅不应该在历史表中同时保留 'status_from' (prevous_status) 和 'status_to',你也不应该在 THINGS 表中保留状态。这违反了标准数据规范化规则。

    您应该做的是从 THINGS 表中删除状态,在两个表之间建立 FK 关系,并在需要时使用适当的查询来导出当前状态。

    首先,创建并填充 THINGS 表。我添加了几列只是为了说明该表中应包含的内容:

    小号

    QL> create table things (thing_id number,
      2                       attrib_a varchar2(10),
      3                       attrib_b varchar2(10),
      4                         constraint thing_pk primary key (thing_id)
      5                      )
      6  ;
    
    Table created.
    
    SQL> insert into things
      2     values (7770,
      3             'red',
      4             'white'
      5            )
      6  ;
    
    1 row created.
    
    SQL> insert into things
      2     values (8880,
      3             'blue',
      4             'green'
      5            )
      6  ;
    
    1 row created.
    

    然后创建并填充历史跟踪表:

    SQL> create table thing_status
      2       (thing_id number,
      3        new_status  varchar2(10),
      4        status_date date,
      5          constraint fk_things
      6            foreign key (thing_id)
      7            references things(thing_id)
      8        )
      9  ;
    
    Table created.
    
    SQL> -- -----------------------------------------------------
    SQL> insert into thing_status
      2     values (7770,
      3             'incomplete',
      4             to_date('2016-08-09 00:00:00','yyyy-mm-dd hh24:mi:ss')
      5             )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status
      2     values (7770,
      3             'inactive',
      4             to_date('2016-08-10 16:26:22','yyyy-mm-dd hh24:mi:ss')
      5             )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status
      2     values (7770,
      3             'active',
      4             to_date('2016-08-11 12:32:04','yyyy-mm-dd hh24:mi:ss')
      5             )
      6  ;
    
    1 row created.
    
    SQL> -- -----------------------------------------------------
    SQL> insert into thing_status
      2     values (8880,
      3             'incomplete',
      4             to_date('2016-08-12 00:00:00','yyyy-mm-dd hh24:mi:ss')
      5             )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status
      2     values (8880,
      3             'inactive',
      4             to_date('2016-08-13 16:26:22','yyyy-mm-dd hh24:mi:ss')
      5             )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status
      2     values (8880,
      3             'active',
      4             to_date('2016-08-14 12:32:04','yyyy-mm-dd hh24:mi:ss')
      5             )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status
      2     values (8880,
      3             'expired',
      4             to_date('2016-08-15 12:32:04','yyyy-mm-dd hh24:mi:ss')
      5             )
      6  ;
    
    1 row created.
    

    还有你的历史报告(和以前一样)

    SQL> -- -----------------------------------------------------
    SQL> --  Select status history
    SQL> --
    SQL> select thing_id,
      2         new_status,
      3         to_char(status_date,'dd-Mon-yyyy hh24:mi:ss') status_start_date,
      4         lead(to_char(status_date,'yyyy-mm-dd hh24:mi:ss'),1)
      5           over (partition by thing_id order by status_date) status_end_date
      6  from thing_status
      7  order by thing_id,
      8           status_date
      9  ;
    
      THING_ID NEW_STATUS STATUS_START_DATE    STATUS_END_DATE
    ---------- ---------- -------------------- -------------------
          7770 incomplete 09-Aug-2016 00:00:00 2016-08-10 16:26:22
          7770 inactive   10-Aug-2016 16:26:22 2016-08-11 12:32:04
          7770 active     11-Aug-2016 12:32:04
          8880 incomplete 12-Aug-2016 00:00:00 2016-08-13 16:26:22
          8880 inactive   13-Aug-2016 16:26:22 2016-08-14 12:32:04
          8880 active     14-Aug-2016 12:32:04 2016-08-15 12:32:04
          8880 expired    15-Aug-2016 12:32:04
    
    7 rows selected.
    

    还有你的现状报告

    SQL> -- -----------------------------------------------------
    SQL> --  Select current status
    SQL> --
    SQL> select t.thing_id,
      2         t.attrib_a,
      3         t.attrib_b,
      4         s.new_status curr_status,
      5         to_char(s.status_date,'yyyy-mm-dd hh24:mi:ss') status_date
      6  from things t
      7   join thing_status s
      8     on t.thing_id = s.thing_id
      9  where s.status_date = (select max(status_date)
     10                         from thing_status x
     11                         where s.thing_id = x.thing_id
     12                         group by thing_id
     13                        )
     14  order by t.thing_id
     15  ;
    
      THING_ID ATTRIB_A   ATTRIB_B   CURR_STATU STATUS_DATE
    ---------- ---------- ---------- ---------- -------------------
          7770 red        white      active     2016-08-11 12:32:04
          8880 blue       green      expired    2016-08-15 12:32:04
    
    • 4
  2. EdStevens
    2016-09-19T09:53:08+08:002016-09-19T09:53:08+08:00
    SQL> create table thing_status_tract (thing_id number,
      2        new_status  varchar2(10),
      3        status_date date
      4       )
      5  ;
    
    Table created.
    
    SQL> insert into thing_status_tract
      2  values (7770,
      3  'incomplete',
      4  to_date('2016-08-09 00:00:00','yyyy-mm-dd hh24:mi:ss')
      5  )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status_tract
      2  values (7770,
      3  'inactive',
      4  to_date('2016-08-09 16:26:22','yyyy-mm-dd hh24:mi:ss')
      5  )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status_tract
      2  values (7770,
      3  'active',
      4  to_date('2016-08-10 12:32:04','yyyy-mm-dd hh24:mi:ss')
      5  )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status_tract
      2  values (8880,
      3  'incomplete',
      4  to_date('2016-08-09 00:00:00','yyyy-mm-dd hh24:mi:ss')
      5  )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status_tract
      2  values (8880,
      3  'inactive',
      4  to_date('2016-08-09 16:26:22','yyyy-mm-dd hh24:mi:ss')
      5  )
      6  ;
    
    1 row created.
    
    SQL> insert into thing_status_tract
      2  values (8880,
      3  'active',
      4  to_date('2016-08-10 12:32:04','yyyy-mm-dd hh24:mi:ss')
      5  )
      6  ;
    
    1 row created.
    
    SQL> select thing_id,
      2      new_status,
      3      to_char(status_date,'dd-Mon-yyyy hh24:mi:ss') status_start_date,
      4      lead(to_char(status_date,'yyyy-mm-dd hh24:mi:ss'),1)
      5        over (partition by thing_id order by status_date) status_end_date
      6  from thing_status_tract
      7  order by thing_id,
      8        status_date;
    
      THING_ID NEW_STATUS STATUS_START_DATE    STATUS_END_DATE
    ---------- ---------- -------------------- -------------------
          7770 incomplete 09-Aug-2016 00:00:00 2016-08-09 16:26:22
          7770 inactive   09-Aug-2016 16:26:22 2016-08-10 12:32:04
          7770 active     10-Aug-2016 12:32:04
          8880 incomplete 09-Aug-2016 00:00:00 2016-08-09 16:26:22
          8880 inactive   09-Aug-2016 16:26:22 2016-08-10 12:32:04
          8880 active     10-Aug-2016 12:32:04
    
    6 rows selected.
    
    • 0

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

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