重置row_number()在另一列中看到1时
说我有这样的桌子。
| 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如我在评论中提到的那样,您需要在此处 订购列;如果您没有一个,那么如果不添加上述列(故事的结尾),那是不可能的。
假设您确实有一列可以通过订购这只是一个差距和岛屿问题:
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:db<>fiddle
也许这样?
(选择0)
可以是任何常数,例如(选择'nothtings')
Perhaps like this?
The
(SELECT 0)
can be any constant, e.g.(SELECT 'anything')