我有一个像这样的数据列表:
ID step date 1 SECOND_ATTEMPT 03/19/2018 1 QC_READY 03/23/2018 1 QC_REJECTS 03/26/2018 2 SCHEDULED 02/01/2018 2 FINISHED 02/04/2018 3 SECOND_ATTEMPT 04/02/2018 3 QC_READY 04/03/2018 4 SECOND_ATTEMPT 01/15/2018 4 FINISHED 01/25/2018
我的查询,被剥离,看起来像这样。
select
j.id,
wfh.step,
wfh.date
from Job j
join work_flow_history wfh on j.id = wfh.job_id
wfh ID对于每个作业中的每个步骤都是唯一的。因此,如果查看步骤ID,它将如下所示:
wfh_ID step
001 SECOND_ATTEMPT
002 QC_READY
etc...
当步骤列包含SECOND_ATTEMPT
并且不包含FINISHED
时,我想返回行中包含每个ID最新日期的行中的所有信息。正确的结果集如下所示:
ID step date 1 QC_REJECTS 03/26/2018 3 QC_READY 04/03/2018
答案 0 :(得分:0)
这是一个有点奇怪的要求。这是一种方法:
select t.*
from t
where not exists (select 1 from t t2 where t2.id = t.id and t2.step = 'FINISHED') and
exists (select 1 from t t2 where t2.id = t.id and t2.step = 'SECOND_ATTEMPT') and
t.date = (select max(t2.date) from t t2 where t2.id = t.id);
只有一个子查询的另一种方式:
select t.*
from t join
(select t.id, max(t.date) as maxd
from t
group by t.id
having sum(t.step = 'SECOND_ATTEMPT') > 0 and
sum(t.step = 'FINISHED') = 0
) tt
on tt.id = t.id and tt.maxd = t.date;
答案 1 :(得分:0)
SELECT *
FROM
(
SELECT
j.id,
wfh.step,
wfh.date
FROM Job j
JOIN work_flow_history wfh on j.id = wfh.job_id
WHERE wfh.step NOT LIKE '%FINISHED%'
AND wfh.step LIKE '%SECOND_ATTEMPT%'
ORDER BY DATE_FORMAT(wfh.date, "%Y-%m-%d") DESC
) t
GROUP BY t.id
GROUP BY在分组时占据第一行,子查询事先对其进行排序。由于不存在任何SUM()或其他影响,因此不需要更复杂的
在数据库中以更好的格式保存日期会避免使用date_format()。
答案 2 :(得分:0)
感谢先前评论中发布的答案,我得到了一些想法,并想出如何使其发挥作用。
select
j.id
,sum(case when wfh.step = 'SECOND_ATTEMPT' then 1 else 0 end) sum_
,(select max(wfh1.date)
from work_flow_history wfh1
join job j1 on j1.id = wfh1.job_id
where j1.id = j.id
order by wfh1.date desc
limit 1) date_
,(select wfh1.step
from work_flow_history wfh1
join job j1 on j1.id = wfh1.job_id
where j1.id = j.ie
and wfh1.step <> 'FINISHED'
order by wfh1.date desc
limit 1) step_
from job j
join wofk_flow_history wfh on j.id = wfh.job_id
group by j.id
此查询成功查看每个作业ID的所有wfh步骤,如果SECOND_ATTEMPT位于与该作业ID关联的任何行中,则返回max wfh步骤。
我知道它真的很乱。我不确定如何将子查询列更改为正确的连接以使其看起来更好,我还需要添加逻辑以便仅包含sum_ = 1的实例。但我很高兴已经超越了这个障碍。谢谢大家的帮助!