如何按顺序连接行而不重复

时间:2020-01-03 21:31:00

标签: mysql

我有一个历史表,该表存储了何时激活或停用调制解调器。 我想将这两行:激活和去激活合并为一。

我尝试了左联接。但是我将每个动作行与每个停用行连接在一起。

Activation
id_history  id_modem  date        Type
============================================
1           2         2017-07-14  Activation
3           2         2018-01-18  Activation


Deactivation
id_history  id_modem   date       Type
==============================================
2           2          2017-09-07 Deactivation
4           2          2018-02-19 Deactivation

我想要这个:

id_history_act  id_modem  date_act    type_act  id_history_dea  date_dea    type_dea
======================================================================================
1               2         2017-07-14  Act       2               2017-09-07  Dea
3               2         2018-01-18  Act       4               2018-02-19  Dea

但是我在做这个的时候得到了这个

select 
    ha.id_history id_history_act,
    ha.id_modem,
    ha.date date_act,
    ha.type type_act,

    hd.id_history id_history_dea,
    hd.date date_dea,
    hd.type type_dea
from 
    history_activation ha 
    left join history_deactivation hd on(hd.id_modem = ha.id_modem);


id_history_act  id_modem  date_act    type_act  id_history_dea  date_dea    tupe_dea
==================================================================================
1               2         2017-07-14  Act       2               2017-09-07  Dea
1               2         2017-07-14  Act       4               2018-02-19  Dea
3               2         2018-01-18  Act       2               2017-09-07  Dea
3               2         2018-01-18  Act       4               2018-02-19  Dea

我尝试使用排名,但是我需要使用变量,并且变量不能在视图中使用。

在激活和停用查询中将count(*)与另一个联接一起使用太慢了。

编辑:这是原始表格

HistoryTable
id_history  id_modem   date         Type 
====================================================
1           2          2017-07-14   Activation
2           2          2017-09-07   Deactivation
3           2          2018-01-18   Activation
4           2          2018-02-19   Deactivation

2 个答案:

答案 0 :(得分:1)

如果ID和日期与示例数据一样一致,则需要进行自我连接,然后为每个id_historydate选择最小值type = Deactivation和最小值type = Activation }:

select
  h1.id_history id_history_act, 
  h1.id_modem, 
  h1.date date_act,
  min(h2.id_history) id_history_des,
  min(h2.date) date_des
from HistoryTable h1 left join HistoryTable h2
on h2.id_modem = h1.id_modem and h2.date > h1.date
where h1.type = 'Activation' and h2.type = 'Deactivation'
group by h1.id_history, h1.id_modem, h1.date

请参见demo
结果:

| id_history_act | id_modem | date_act            | id_history_des | date_des            |
| -------------- | -------- | ------------------- | -------------- | ------------------- |
| 1              | 2        | 2017-07-14 00:00:00 | 2              | 2017-09-07 00:00:00 |
| 3              | 2        | 2018-01-18 12:03:14 | 4              | 2018-02-19 12:15:07 |

答案 1 :(得分:0)

您只需要在查询中使用ORDER BY子句

尝试一下:

select 
ha.id_history id_history_act,
ha.id_modem,
ha.date date_act,

hd.id_history id_history_des,
hd.date date_des,
from 
history_activation ha 
left join history_desctivation hd on(hd.id_modem = ha.id_modem);
order by date_act