我有这个查询
SELECT
schedule.JOB_NUMBER, max(schedule_milestone.actual_start_date), milestone.milestone_name
FROM
schedule
inner join
schedule_milestone on (schedule.schedule_id = schedule_milestone.schedule_id) inner join
milestone on (milestone.milestone_id = schedule_milestone.milestone_id)
--where schedule.job_number = '024MGV002' using this for testing
group by schedule.job_number, milestone.milestone_name
我返回jobNumber,MaxDate和里程碑名称。
我想要它返回的是jobnumber,MaxDate和里程碑名称,但我只想为每个作业编号添加一行。我想要一个具有最新日期的里程碑。
因此,工作号码1234有4个不同的里程碑。我想要具有最大日期的里程碑的作业编号,日期和里程碑名称。
答案 0 :(得分:1)
如果您没有使用MySQL,可以使用ROW_NUMBER()
功能
否则
SELECT
S.JOB_NUMBER
, SM.actual_start_date
, M.milestone_name
FROM schedule S
JOIN schedule_milestone SM
ON S.schedule_id = SM.schedule_id
JOIN milestone M
ON M.milestone_id = SM.milestone_id
WHERE SM.actual_start_date = ( SELECT MAX(SM.actual_start_date)
FROM schedule iS
JOIN schedule_milestone iSM
ON iS.schedule_id = iSM.schedule_id
JOIN milestone iM
ON iM.milestone_id = iSM.milestone_id
WHERE iS.job_number = S.job_number
)
请注意,如果多个里程碑共享同一start_date
的{{1}}
答案 1 :(得分:0)
select * from (
Select JOB_NUMBER,dt,milestone_name,row_number() over (partition by JOB_NUMBER order by dt desc ) as rownm
from (
SELECT
schedule.JOB_NUMBER as JOB_NUMBER, milestone.milestone_name, max(schedule_milestone.actual_start_date) as dt
FROM
schedule schedule
inner join
schedule_milestone schedule_milestone
on (schedule.schedule_id = schedule_milestone.schedule_id)
inner join
milestone milestone on
(milestone.milestone_id = schedule_milestone.milestone_id)
group by schedule.job_number, milestone.milestone_name))
where rownm=1;