Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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 根据条件获取最新的行_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

Sql 根据条件获取最新的行

Sql 根据条件获取最新的行,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我有一张表资料—— 这里每个ID可以有多个修订。我想采取最新的修订(即最高的001002003等)。但是,如果最新版本的curr为NULL(字符串)或已撤销,则我将采用上一版本及其相应的值。如果即使是curr也是NULL或撤销我也必须再次转到上一版本。如果所有的修订都有相同的问题,那么我们可以忽略它。因此,预期的输出是 +--------+-----+------------------+---------------+-------+ | ID | REV | name

我有一张表
资料
——

这里每个ID可以有多个修订。我想采取最新的修订(即最高的001002003等)。但是,如果最新版本的
curr
NULL
(字符串)或
已撤销
,则我将采用上一版本及其相应的值。如果即使是
curr
也是
NULL
撤销
我也必须再次转到上一版本。如果所有的修订都有相同的问题,那么我们可以忽略它。因此,预期的输出是

+--------+-----+------------------+---------------+-------+
| ID     | REV | name             | Description   | curr  |
+--------+-----+------------------+---------------+-------+
| 211-32 | 001 | Screw 1.0        | Used in MAT 1 | READY |
| 212-41 | 002 | BOLT H2+Form     | Heavy solid   | READY |
| 101-24 | 002 | HexHead Spl      | NOR-22        | READY |
| 423-98 | 001 | Nut Repair spare | NORM1         | READY |
+--------+-----+------------------+---------------+-------+
我已经尝试了下面的代码,但我不知道如何进行以前的修订

with cte as (
select *,dense_rank() over (partition by id order by rev desc) as DR ,
lead(curr) over (partition by id order by rev desc) LEAD_CURR
from material )
select * from cte where DR = 1 and curr='READY'
union all
select * from cte where LEAD_CURR='READY' and DR=2
union all
select * from cte where LEAD_CURR='READY' and DR=3

这听起来像是过滤然后计算行号:

select m.*
from (select m.*,
             row_number() over (partition by id order by rev desc) as seqnum
      from material m
      where curr is not null and curr not in ( 'WITHDRAWN', 'NULL' ) 
     ) m
where seqnum = 1;
也可以使用相关子查询执行此操作:

select m.*
from material m
where m.rev = (select max(m2.rev)
               from material m2
               where m2.id = m.id and
                     curr is not null and curr not in ( 'WITHDRAWN', 'NULL' ) 
              );
他是一把小提琴

注意:将字符串
'NULL'
存储在列中是非常不传统的,因为这很容易与SQL“constant”
NULL
混淆


此外,您的问题特别提到了
“撤回”
NULL
,但没有说明允许使用哪些其他值。显然,上面查询中的逻辑可能等价于
curr='READY'
,您可以使用它。上述逻辑遵循您对问题的描述。

OP希望获得具有最新版本和以前(
LEAD(curr)
)值的零件。@MaciejLos。我不知道你的评论是指什么。如果您查看DBFIDLE中的结果,您将看到它们正是OP在问题中的结果。我的评论是指OP问题。仔细看看。跟随提问者张贴的小提琴。你错过了那部分:
lead(curr)over(按id顺序划分,按rev desc)lead\u curr
非常感谢:)@GordonLinoff
select m.*
from material m
where m.rev = (select max(m2.rev)
               from material m2
               where m2.id = m.id and
                     curr is not null and curr not in ( 'WITHDRAWN', 'NULL' ) 
              );