row_number跳过值

发布于 2025-01-24 10:07:40 字数 295 浏览 2 评论 0 原文

我有一个这样的表:

“

当我“ inclate” include“ in Column inclage> include_appt) ,当它找到空时,它应该跳过设置为“ null”或“ 0”,并且在下一个找到的“ nuff”中,“返回”到计数停止的位置。

上面的屏幕截图几乎可以做到,但不幸的是,计数并未重置下一个值。

PS:我不能使用对分区的使用

I have a table like this:

1

The idea were to count only when I have "Include" at column include_appt, when it finds NULL, it should skip set is as "NULL" or "0" and on next found "Include" back to counting where it stopped.

The screenshot above I was almost able to do it but unfortunately the count didn't reset on next value.

PS: I can't use over partition because I have to keep the order by id ASC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆躺在深渊里 2025-01-31 10:07:41

如果您试图防止为null/0值添加行号,为什么不尝试这样的查询呢?

SELECT
    row_num AS id,
    include_appt,
    ROW_NUMBER() OVER
    (
        ORDER BY (SELECT 0)
    ) AS row_num2
FROM C
WHERE ISNULL(C.include_appt, 0) <> 0
ORDER BY row_num

我建议您重新考虑要在最终结果中显示的列名/别名,以避免混乱,但是上述应有效地完成您想要的工作。

If you are trying to prevent row numbers being added for NULL/0 values, why not try a query like this instead?

SELECT
    row_num AS id,
    include_appt,
    ROW_NUMBER() OVER
    (
        ORDER BY (SELECT 0)
    ) AS row_num2
FROM C
WHERE ISNULL(C.include_appt, 0) <> 0
ORDER BY row_num

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.

长伴 2025-01-31 10:07:41

您需要条款的分区

SELECT
    row_num AS id,
    include_appt,
    CASE WHEN include_appt IS NULL
      THEN 0
      ELSE
        ROW_NUMBER() OVER (PARTITION BY include_appt ORDER BY (SELECT 0))
      END AS row_num2
FROM C
ORDER BY row_num

You need a PARTITION BY clause

SELECT
    row_num AS id,
    include_appt,
    CASE WHEN include_appt IS NULL
      THEN 0
      ELSE
        ROW_NUMBER() OVER (PARTITION BY include_appt ORDER BY (SELECT 0))
      END AS row_num2
FROM C
ORDER BY row_num
情域 2025-01-31 10:07:41
SELECT id, include_appt,
CASE WHEN include_appt IS NULL THEN 0
  ELSE ROW_NUMBER() OVER (PARTITION BY include_appt ORDER BY id ASC)
  END AS row_num 
FROM #1 ORDER BY id asc
SELECT id, include_appt,
CASE WHEN include_appt IS NULL THEN 0
  ELSE ROW_NUMBER() OVER (PARTITION BY include_appt ORDER BY id ASC)
  END AS row_num 
FROM #1 ORDER BY id asc
过潦 2025-01-31 10:07:41

如下另一个答案中,可以通过include_appt 通过分区轻松完成此操作,但是在播放查询计划之后,我认为仍然值得考虑这种略有不同的方法,这可能会提供一个可能提供的方法性能提升。我相信,通过能够使用群集索引而没有涉及 flag 列:

select id, flag,
    case when flag is not null
        then row_number() over (order by id)
               - count(case when flag is null then 1 end) over (order by id)
        else 0 end /* count up the skips */ as new_rn
from T
order by id

示例(包括“重置”行为):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 the flag column:

select id, flag,
    case when flag is not null
        then row_number() over (order by id)
               - count(case when flag is null then 1 end) over (order by id)
        else 0 end /* count up the skips */ as new_rn
from T
order by id

Examples (including a "reset" behavior): https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=c9f4c187c494d2a402e43a3b24924581

Performance comparison:
https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=719f7bd26135ab498d11c786f1b1b28b

心意如水 2025-01-31 10:07:40

我建议将dense_rank()与您隐藏的列( - *,)使用:

SELECT
    row_num AS id,
    include_appt,
    CASE WHEN include_appt is not null
    THEN ROW_NUMBER() OVER(ORDER BY (SELECT 0)) 
        + 1 
        - DENSE_RANK() OVER(
            PARTITION BY /*some hidden columns*/ 
            ORDER BY/*some hidden columns*/) 
    ELSE NULL
    END AS row_num2
FROM C
ORDER BY row_num

然后结果为:
在此处输入图像描述

I suggest using the DENSE_RANK() with the columns you have hidden (--*,):

SELECT
    row_num AS id,
    include_appt,
    CASE WHEN include_appt is not null
    THEN ROW_NUMBER() OVER(ORDER BY (SELECT 0)) 
        + 1 
        - DENSE_RANK() OVER(
            PARTITION BY /*some hidden columns*/ 
            ORDER BY/*some hidden columns*/) 
    ELSE NULL
    END AS row_num2
FROM C
ORDER BY row_num

Then the result will be:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文