Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 获取指定列为null的第一组行_Sql_Sql Server_Database_Tsql - Fatal编程技术网

Sql 获取指定列为null的第一组行

Sql 获取指定列为null的第一组行,sql,sql-server,database,tsql,Sql,Sql Server,Database,Tsql,我有一个审批表,其中有一个请求者、验证者和审批者。有时,在某个请求中有多个验证器否: RequestNo | UserName | Action | Seq | ActionDate R001 | JohnD | Requestor | 1 | 01/01/2017 R001 | SamS | Verifier | 2 | NULL R001 | TrishL | Verifier | 3 | NULL R001

我有一个审批表,其中有一个请求者、验证者和审批者。有时,在某个请求中有多个验证器否:

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中获取第一个值为null的操作组(请求者、验证者、批准者)。我需要在ActionDate为null的第一个Action组下显示用户名和Action。在这种情况下,应该是SamS、TrishL和GeorgeP。谢谢大家

像这样的查询应该可以得到您想要的信息:

Select UserName, Action
From ActionGroups
Where Action in (
  Select top 1 Action 
  from ActionGroups 
  Where ActionDate Is Null
  Order by Action
)

在本例中,您通过一个子查询进行过滤,该子查询返回第一个操作(按字母顺序排序),该操作的日期为null
ActionDate

,这有点难看,但应该可以得到所需的内容。首先将具有相同操作的连续行分配给组。然后获取每组的
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

是否有可能是另一行,如
R001 | BlaB | Verifier | 6 | NULL
?只有一行有ActionDate吗?嗨,没有,那是不可能的。审批人将始终是请求中的最后一行如果验证者组中第一个空值之后的行之一具有非空actionDate?您还必须显示验证器组中的所有行吗?然后它将查找下一个ActionDate为空的组。假设来自
Verifier
组的TrishL已填充ActionDate,查询现在应显示来自Approver组的JackF谢谢!我在内部和外部查询中都放置了一个
WHERE RequestNo='R001'
语句,以获得所需的结果。这是好的sql代码吗?因为如果我将WHERE语句放在SELECT内部,它将重复验证多次。如果我将WHERE语句放在外部SELECT中,它将只显示approver@HarambeAttackHelicopter令人惊叹的。如果这有助于你解决问题,请考虑接受答案,当一个组中只有空值时,你不必检查MIN和MAX:
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)