我正在尝试编写一个查询,以查找订单7658(表名称 - ECOM_DATA)状态从15到20之间的天数差异
ORD_NO INT_ORD_LINE_STAT_EXP STAT_DATE_ACT STAT_DATE_EXP
7658 15 11-OCT-17 00:00:00 11-OCT-17 00:00:00
7658 20 16-OCT-17 00:00:00 16-OCT-17 00:00:00
有人可以建议吗?感谢您的帮助。
答案 0 :(得分:0)
最小化设置:
create table ecom_data
(
ord_no integer,
int_ord_line_stat_exp integer,
stat_date_act timestamp
);
insert into ecom_data (ord_no, int_ord_line_stat_exp, stat_date_act) values (7658, 15, '2017-10-11');
insert into ecom_data (ord_no, int_ord_line_stat_exp, stat_date_act) values (7658, 20, '2017-10-16');
查询:
select
a.ord_no,
datediff(b.stat_date_act, a.stat_date_act) as days_between
from
(
select
ord_no,
stat_date_act
from
ecom_data
where
int_ord_line_stat_exp = 15
) a
join
(
select
ord_no,
stat_date_act
from
ecom_data
where
int_ord_line_stat_exp = 20
) b
on a.ord_no = b.ord_no
where
a.ord_no = 7658;
结果:
+--------+--------------+
| ord_no | days_between |
+--------+--------------+
| 7658 | 5 |
+--------+--------------+
1 row in set (0.01 sec)