row_number跳过值
我有一个这样的表:
当我“ inclate” include“ in Column inclage> include_appt)
,当它找到空时,它应该跳过设置为“ null”或“ 0”,并且在下一个找到的“ nuff”中,“返回”到计数停止的位置。
上面的屏幕截图几乎可以做到,但不幸的是,计数并未重置下一个值。
PS:我不能使用对分区的使用
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您试图防止为null/0值添加行号,为什么不尝试这样的查询呢?
我建议您重新考虑要在最终结果中显示的列名/别名,以避免混乱,但是上述应有效地完成您想要的工作。
If you are trying to prevent row numbers being added for NULL/0 values, why not try a query like this instead?
I would recommend reconsidering the column names/aliases you want to have displayed in your final result to avoid confusion, but the above should effectively do what you are wanting.
您需要条款的
分区
You need a
PARTITION BY
clause如下另一个答案中,可以通过include_appt 通过
分区轻松完成此操作,但是在播放查询计划之后,我认为仍然值得考虑这种略有不同的方法,这可能会提供一个可能提供的方法性能提升。我相信,通过能够使用群集索引而没有涉及
flag
列:示例(包括“重置”行为):https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=c9f4c187c494d2a402e43a3b24924581
Performance comparison:
This can be easily done with a
partition by include_appt
as in another answer below, yet after playing around with the query plans I've decided that it is still worthwhile to consider this slightly different approach which might offer a performance boost. I believe the benefit is gained by being able to use the clustered index without involving a sort on theflag
column:Examples (including a "reset" behavior): https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=c9f4c187c494d2a402e43a3b24924581
Performance comparison:
https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=719f7bd26135ab498d11c786f1b1b28b
我建议将dense_rank()与您隐藏的列( - *,)使用:
然后结果为:
在此处输入图像描述
I suggest using the DENSE_RANK() with the columns you have hidden (--*,):
Then the result will be:
enter image description here