所以我正在运行以下查询:
Select a.Location, ProcType, Status, f.Code, p.Day, Start, Stop,
(Case When Stop IS NULL then Start else Stop end) as LastState,
(case when Type like 'Type1%' then Status else 'NULL' end) as Type1,
(case when Type like 'Type2%' then Status else 'NULL' end) as Type2
from dbo.table1 a
inner join Database..table2 p on Name = 'RunDay' and
a.Location = p.Location and p.Value = a.SomeRunDay
left outer join [Database].[dbo].[table3] f on a.File_number =
f.File_number
where a.Location is not NULL
ORDER BY Location
产生的结果是: **此表仅显示一个位置的值,我在表格中有数百个
------------------------------------------------------------------------------|Location|ProcType|Status|Code| Day | Start | Stop |LastState|Type1|Type2|
------------------------------------------------------------------------------
|1 |Type1_A|Done| A |2018/03/01|15:58|16:32|2018/03/01 16:32|Done|NULL|
------------------------------------------------------------------------------
|1 |Type1_B|Done| B |2018/03/01|15:59|16:32|2018/03/01 16:34|Done|NULL|
------------------------------------------------------------------------------
|1 |Type2_A_Loop|Done| A |2018/03/01|16:00|16:38|2018/03/01 16:38|NULL|Done|
--------------------------------------------------------------------------------
|1 |Type1_A_Loop|Processing| B |2018/03/11|15:59|16:32|2018/03/11 16:34|NULL|Processing|
--------------------------------------------------------------------------------
|1 |Type2_B|Processing| B |2018/03/11|15:59|16:32|2018/03/11 16:34|NULL|Processing|
------------------------------------------------------------------------
我正试图找到一种方法来仅查看显示的最新ProcType
Processing
考虑到LastState
列显示的是最新状态。换句话说,我想看到仅包含Processing
状态的'loop'的结果。
在我脑海中运行的解决方案就像是
Select MAX (LastState) from dbo.table1
Where Status = 'Done'
并执行某种联盟..?我需要一些帮助。
由于
答案 0 :(得分:0)
select *
from ( Select a.Location, ProcType, Status, f.Code, p.Day, Start, Stop,
isnull(Stop, Start) as LastState,
case when Type like 'Type1%' then Status else 'NULL' end as Type1,
case when Type like 'Type2%' then Status else 'NULL' end as Type2,
row_number() over (partition by a.Location order by isnull(Stop, Start) desc) as rn
from dbo.table1 a
inner join Database..table2 p
on Name = 'RunDay'
and Status = 'Done'
and a.Location = p.Location
and p.Value = a.SomeRunDay
left outer join [Database].[dbo].[table3] f
on a.File_number = f.File_number
) tt
where tt.rn = 1
ORDER BY tt.Location