将if语句与row_number一起使用,然后使用
我已经尝试创建下面的代码,但对我不起作用。
我要做的是使用row_number,但是请检查列MP ='yes',然后+1 to row_number值,如果它确实可以给我row_number。
SELECT
[Key]
,[Key2]
,MP
,OriginalOrder = ROW_NUMBER() OVER(Partition by [Key] ORDER BY [Key],[Key2])
,IIF(MP = 'Yes' , ROW_NUMBER() OVER(Partition by [Key] ORDER BY [Key],[Key2])+1,ROW_NUMBER() OVER(Partition by [Key] ORDER BY [Key],[Key2]))what_i_have_tried
FROM
cte_RowNumbertest
ORDER BY
1,2
I have tried creating the code below but it didn't work for me.
what I'm trying to do is use Row_number however check if the column MP =' Yes' then +1 to the Row_number value if it does otherwise just give me the Row_number.
SELECT
[Key]
,[Key2]
,MP
,OriginalOrder = ROW_NUMBER() OVER(Partition by [Key] ORDER BY [Key],[Key2])
,IIF(MP = 'Yes' , ROW_NUMBER() OVER(Partition by [Key] ORDER BY [Key],[Key2])+1,ROW_NUMBER() OVER(Partition by [Key] ORDER BY [Key],[Key2]))what_i_have_tried
FROM
cte_RowNumbertest
ORDER BY
1,2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解,则可以尝试使用
sum
window>在时使用时,如果mp
是YES是添加一个。If I understand correctly, you can try to use
SUM
window function withCASE WHEN
, ifMP
is yes add one more.i think 您实际想要的是
row_number
和条件运行count
:I think what you actually want is a
ROW_NUMBER
and a conditional runningCOUNT
: