我有一个Approval表,其中有一个请求者,验证者和批准者。有时在某个请求中有多个验证者:
RequestNo | UserName | Action | Seq | ActionDate
R001 | JohnD | Requestor | 1 | 01/01/2017
R001 | SamS | Verifier | 2 | NULL
R001 | TrishL | Verifier | 3 | NULL
R001 | GeorgeP | Verifier | 4 | NULL
R001 | JackF | Approver | 5 | NULL
我需要的是在ActionDate中获取第一个具有空值的Action组(Requestor,Verifier,Approver)。我需要在第一个具有null ActionDate的Action组下显示UserNames和Action。在这种情况下,它应该是SamS,TrishL和GeorgeP。谢谢你们!
答案 0 :(得分:1)
这样的查询可以为您提供所需的信息:
Select UserName, Action
From ActionGroups
Where Action in (
Select top 1 Action
from ActionGroups
Where ActionDate Is Null
Order by Action
)
在这种情况下,您将通过子查询进行过滤,该子查询返回具有空ActionDate
的第一个Action(按字母顺序排序)。
答案 1 :(得分:1)
这有点难看,但应该得到你需要的东西。首先将具有相同操作的连续行分配给组。然后获取每个组的max
和min
日期。然后检查最大和最小日期是否都为空并选择第一个这样的组。
select requestno,username,action,seq,actiondate
from (select t.*
,min(case when maxdate is null and mindate is null then grp end) over(partition by requestno) as first_grp
from (select t.*
,max(actiondate) over(partition by requestno,grp) as maxdate
,min(actiondate) over(partition by requestno,grp) as mindate
from (select t.*,sum(col) over(partition by requestno order by seq) as grp
from (select t.*
,case when lag(action) over(partition by requestno order by seq)=action then 0 else 1 end as col
from t
) t
) t
)t
) t
where first_grp=grp
答案 2 :(得分:1)
Try using the below query.
declare @tbl table
(
ReqNO varchar(10),
Username varchar(10),
[Action] varchar(20),
Seq bit,
[Date] datetime null
)
insert into @tbl values ('R001','JohnD','Requestor',1,'01/01/2017')
insert into @tbl values ('R001','SamS','Verifier',2,null)
insert into @tbl values ('R001','TrishL','Verifier',3,null)
insert into @tbl values ('R001','GeorgeP','Verifier',4,null)
insert into @tbl values ('R001','JackF','Approver',5,null)
--select * from @tbl
select t.Username from @tbl t where t.Action in(
select d.Action from @tbl d where d.Date is null
--and ReqNO='R001'
group by (d.Action) having COUNT(d.Action)>1)