重置row_number()在另一列中看到1时

发布于 2025-02-09 03:27:24 字数 2051 浏览 1 评论 0原文

说我有这样的桌子。

| Date       | Condition |
|------------|-----------|
| 2021-12-24 | 0         |
| 2021-12-24 | 0         |
| 2021-12-24 | 0         |
| 2021-12-24 | 0         |
| 2021-12-24 | 1         |
| 2021-12-24 | 0         |
| 2021-12-24 | 0         |
| 2021-12-25 | 0         |
| 2021-12-25 | 0         |
| 2021-12-25 | 0         |

我想将row_number添加到它,但是当它看到iscon = 1或更改的日期时,我希望将其重置为1,所以它就是这样。

| Date       | Condition | row_number |
|------------|-----------|------------|
| 2021-12-24 | 0         | 1          |
| 2021-12-24 | 0         | 2          |
| 2021-12-24 | 0         | 3          |
| 2021-12-24 | 0         | 4          |
| 2021-12-24 | 1         | 1          |
| 2021-12-24 | 0         | 2          |
| 2021-12-24 | 0         | 3          |
| 2021-12-25 | 0         | 1          |
| 2021-12-25 | 0         | 2          |
| 2021-12-25 | 0         | 3          |

我尝试了

    WITH cte AS
(
    SELECT *
        ,ROW_NUMBER() OVER (PARTITION BY [isCon],[date],[grp] ORDER BY [date]) AS rn
    FROM (select t.*,
             (  
                row_number() over (order by date) -
                row_number() over (partition by isCon,date order by date)
              ) as grp
            from @myTablevariable t
          ) t
    
)
SELECT * FROM cte;

它很接近,但不太正确。 ROW_NUMBER重置为1,无论看到0或1,我都希望它仅在看到1时重置。

| Date       | Condition | row_number |
|------------|-----------|------------|
| 2021-12-24 | 0         | 1          |
| 2021-12-24 | 0         | 2          |
| 2021-12-24 | 0         | 3          |
| 2021-12-24 | 0         | 4          |
| 2021-12-24 | 1         | 1          |
| 2021-12-24 | 0         | 1          |
| 2021-12-24 | 0         | 2          |
| 2021-12-25 | 0         | 1          |
| 2021-12-25 | 0         | 2          |
| 2021-12-25 | 0         | 3          |

请注意,我在SQL Server 2008上,我不可能升级。因此,诸如lag()函数或sum> sum(column1)之类的东西(顺序by)对我来说是不可行的。

Say I have table like this.

| Date       | Condition |
|------------|-----------|
| 2021-12-24 | 0         |
| 2021-12-24 | 0         |
| 2021-12-24 | 0         |
| 2021-12-24 | 0         |
| 2021-12-24 | 1         |
| 2021-12-24 | 0         |
| 2021-12-24 | 0         |
| 2021-12-25 | 0         |
| 2021-12-25 | 0         |
| 2021-12-25 | 0         |

I want to add row_number to it, but when it see isCon = 1 or the date changed, I want it to reset to 1, so it would be like this.

| Date       | Condition | row_number |
|------------|-----------|------------|
| 2021-12-24 | 0         | 1          |
| 2021-12-24 | 0         | 2          |
| 2021-12-24 | 0         | 3          |
| 2021-12-24 | 0         | 4          |
| 2021-12-24 | 1         | 1          |
| 2021-12-24 | 0         | 2          |
| 2021-12-24 | 0         | 3          |
| 2021-12-25 | 0         | 1          |
| 2021-12-25 | 0         | 2          |
| 2021-12-25 | 0         | 3          |

I tried

    WITH cte AS
(
    SELECT *
        ,ROW_NUMBER() OVER (PARTITION BY [isCon],[date],[grp] ORDER BY [date]) AS rn
    FROM (select t.*,
             (  
                row_number() over (order by date) -
                row_number() over (partition by isCon,date order by date)
              ) as grp
            from @myTablevariable t
          ) t
    
)
SELECT * FROM cte;

It's close but not quite right. Row_number reset to 1 no matter it sees 0 or 1, but I want it to reset only when seeing 1.

| Date       | Condition | row_number |
|------------|-----------|------------|
| 2021-12-24 | 0         | 1          |
| 2021-12-24 | 0         | 2          |
| 2021-12-24 | 0         | 3          |
| 2021-12-24 | 0         | 4          |
| 2021-12-24 | 1         | 1          |
| 2021-12-24 | 0         | 1          |
| 2021-12-24 | 0         | 2          |
| 2021-12-25 | 0         | 1          |
| 2021-12-25 | 0         | 2          |
| 2021-12-25 | 0         | 3          |

Please note that I am on SQL server 2008 and it is not possible for me to upgrade. So anything like LAG() function or SUM(column1) OVER (ORDER BY) is not viable for me.

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

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

发布评论

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

评论(2

夏末染殇 2025-02-16 03:27:24

正如我在评论中提到的那样,您需要在此处 订购列;如果您没有一个,那么如果不添加上述列(故事的结尾),那是不可能的。

假设您确实有一列可以通过订购这只是一个差距和岛屿问题:

WITH CTE AS(
    SELECT YourAlwaysAscendingColumn,
           [date],
           Condition,
           ROW_NUMBER() OVER (PARTITION BY [Date] ORDER BY YourAlwaysAscendingColumn) - 
           ROW_NUMBER() OVER (PARTITION BY [Date], Condition ORDER BY YourAlwaysAscendingColumn) AS Grp
    FROM dbo.YourTable)
SELECT [date],
       Condition,
       ROW_NUMBER() OVER (PARTITION BY [Date], Grp ORDER BY YourAlwaysAscendingColumn) AS RowNumber
FROM CTE
ORDER BY [date],
         YourAlwaysAscendingColumn;

db<> fiddle

As I mentioned in the comments, you need a column to ORDER BY here; if you don't have one then what you are after is impossible without adding said column (end of story).

Assuming you do have a column you can ORDER BY this is just a gaps and island problem:

WITH CTE AS(
    SELECT YourAlwaysAscendingColumn,
           [date],
           Condition,
           ROW_NUMBER() OVER (PARTITION BY [Date] ORDER BY YourAlwaysAscendingColumn) - 
           ROW_NUMBER() OVER (PARTITION BY [Date], Condition ORDER BY YourAlwaysAscendingColumn) AS Grp
    FROM dbo.YourTable)
SELECT [date],
       Condition,
       ROW_NUMBER() OVER (PARTITION BY [Date], Grp ORDER BY YourAlwaysAscendingColumn) AS RowNumber
FROM CTE
ORDER BY [date],
         YourAlwaysAscendingColumn;

db<>fiddle

青春如此纠结 2025-02-16 03:27:24

也许这样?

DECLARE
    @TestData TABLE
    (
        SomeDate DATE,
        Condition INT
    );

INSERT INTO @TestData
VALUES
('20211224', 0),
('20211224', 0),
('20211224', 0),
('20211224', 0),
('20211224', 0),
('20211224', 1),
('20211224', 0),
('20211224', 0),
('20211225', 0),
('20211225', 0),
('20211225', 0)

SELECT
    SomeDate,
    Condition,
    ROW_NUMBER() OVER (PARTITION BY SomeDate, Condition ORDER BY (SELECT 0))
FROM
    @TestData;

(选择0)可以是任何常数,例如(选择'nothtings')

Perhaps like this?

DECLARE
    @TestData TABLE
    (
        SomeDate DATE,
        Condition INT
    );

INSERT INTO @TestData
VALUES
('20211224', 0),
('20211224', 0),
('20211224', 0),
('20211224', 0),
('20211224', 0),
('20211224', 1),
('20211224', 0),
('20211224', 0),
('20211225', 0),
('20211225', 0),
('20211225', 0)

SELECT
    SomeDate,
    Condition,
    ROW_NUMBER() OVER (PARTITION BY SomeDate, Condition ORDER BY (SELECT 0))
FROM
    @TestData;

The (SELECT 0) can be any constant, e.g. (SELECT 'anything')

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