选择两列中的单个字段(Mysql)

选择两列中的单个字段(Mysql),mysql,sql,Mysql,Sql,我们使用的是Mysql数据库,我们有一个表,即employee\u transaction 员工事务处理表 id | employee_id | date | transaction_type ------------------------------------------------------ 1 | 1 | 2015-01-01 | 1 2 | 1 | 2015-07-01 | 1 3 |

我们使用的是Mysql数据库,我们有一个表,即employee\u transaction

员工事务处理表

id |  employee_id | date            | transaction_type
------------------------------------------------------
1  | 1            | 2015-01-01      | 1
2  | 1            | 2015-07-01      | 1 
3  | 1            | 2015-12-31      | 0
4  | 2            | 2014-02-01      | 1 
5  | 1            | 2016-04-01      | 1
6  | 2            | 2014-11-30      | 0
7  | 1            | 2016-08-01      | 1
8  | 1            | 2016-10-31      | 0 
等等

要获得正确的结果,需要满足以下条件:-

  • 任何员工的第一个交易类型始终为1
  • 任何员工都不能有连续的事务处理\u type=0
  • 两个连续交易类型的结果如下。这些 结果针对一名员工

  • 如果两个交易类型均为1。 然后开始日期=第一个交易日期,结束日期=第二个交易日期-1,交易类型=1
  • 如果第一笔交易类型为1,下一笔交易类型为0。然后 开始日期=第一个交易日期,结束日期=第二个交易日期 和事务类型=1
  • 如果第一笔交易类型为0,下一笔交易类型为1。然后 开始日期=第一笔交易日期+1,结束日期=第二笔交易 日期-1和交易类型=0
  • 如果在事务\u type=0之后未找到任何事务。则 开始日期=第一笔交易日期+1,结束日期=空且 事务类型=0
我们需要查询employee\u事务表中的以下输出

  employee_id |  start_date     | end_date       | transaction_type
------------------------------------------------------
 1            | 2015-01-01      | 2015-06-30     | 1
 1            | 2015-07-01      | 2015-12-31     | 1 
 1            | 2016-01-01      | 2016-03-31     | 0
 1            | 2016-04-01      | 2016-07-31     | 1
 1            | 2016-08-01      | 2016-10-31     | 1
 1            | 2016-11-01      | (Null)         | 0 
 2            | 2014-02-01      | 2014-11-30     | 1
提前谢谢


如果您有任何问题/需要澄清,请回复我。

使用相关子查询获取每个员工id的下一个日期、下一个交易类型(按日期排序)。然后根据所述条件使用一个
case
表达式

select employee_id
,case when (transaction_type=0 and nxt_t_type=1) or (transaction_type=0 and nxt_t_type is null) then dt+interval '1' day
 else dt
 end as dt
,case when nxt_t_type=1 then nxt_dt-interval '1' day 
      when transaction_type=1 and nxt_t_type=0 then nxt_dt
      when transaction_type=0 and nxt_t_type is null then null
 end as nxt_dt
,transaction_type
from (select t1.*
      ,(select t2.dt from t t2 where t1.employee_id=t2.employee_id and t2.dt > t1.dt 
        order by t2.dt limit 1) as nxt_dt
      ,(select t2.transaction_type from t t2 where t1.employee_id=t2.employee_id and t2.dt > t1.dt 
        order by t2.dt limit 1) as nxt_t_type
      from t t1
     ) x

这里有一个查询,使用相关子查询连接每个员工的连续日期和状态信息:

select
    f.emp_id, f.trans_date, f.trans_type,
    (select trans_date from emp_trans
    where emp_id=f.emp_id and trans_date = (select min(trans_date)
                                       from emp_trans where emp_id=f.emp_id
                                       and trans_date > f.trans_date)
    ) as end_trans_date,
    (select trans_type from emp_trans
    where emp_id=f.emp_id and trans_date = (select min(trans_date)
                                       from emp_trans where emp_id=f.emp_id
                                       and trans_date > f.trans_date)
    ) as end_trans_type
from emp_trans as f;

我删除了不兼容的数据库标记。请标记您真正使用的数据库。此外,使用您提供的数据
orderbyemployee\u id,start\u date)
。规则背后的原因有点不清楚。这些交易意味着什么?嗨@GordonLinoff,我已经更新了问题中的所有细节。谢谢你的建议。参考这个:非常感谢你的回答。我知道这是糟糕的数据库设计,但改变了数据库。如果我们能得到结果。这对我们真的很有帮助。您好,在提供的场景中,日期在订单中,如果他们不在订单中,那么我们需要做什么。我们需要你提供给我的结果。提前谢谢。