当前位置: 代码迷 >> Sql Server >> MY SQL查询
  详细解决方案

MY SQL查询

热度:77   发布时间:2016-04-24 08:53:07.0
MY SQL查询求助
有表work_orders:
project_id step_id created_at
27983         523         2015-9-18 16:49
12981         133         2015-9-17 16:49
25145         523         2015-9-18 16:49
27983         523    2015-9-15 16:49
27983         624         2015-9-11 16:49
27983         535         2015-9-18 16:49
31266         528         2015-9-18 16:49

希望得到如下的结果:
project_id subcloning earliest_created_at
27983         YES                2015-9-11 16:49
12981
25145                         2015-9-18 16:49
31266         YES

结果说明:若project_id对应的step_id是535或者528则subcloning项显示YES;step_id为523或者624的最早的那个created_at为earliest_created_at

------解决思路----------------------
select project_id
,subcloning=case when exists(select 1 from work_orders where project_id=t.project_id 
and step_id in (535,528)) then 'YES' else '' end
,earliest_created_at=(select MIN(created_at) from work_orders where project_id=t.project_id
and step_id in (523,624))
from work_orders t
group by project_id
  相关解决方案