在论坛中出现的比较难的sql问题:29(row_number函数 组内某列的值连续出现3次标记出来)

在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。



组内某列的值连续出现3次标记出来

如下,eid为人员ID,对于同一个EID,date列连续出现3次以上的,FLAG列标记为1.
尽量用1条UPDATE语句!

eid date    flag
1 2013-3-1
1 2013-3-2
1 2013-3-4
1 2013-3-5
1 2013-3-7 1
1 2013-3-8 1
1 2013-3-9 1
1 2013-3-11
1 2013-3-12
1 2013-3-14 1
1 2013-3-15 1
1 2013-3-16 1
1 2013-3-18
2 2013-3-1
2 2013-3-2
2 2013-3-4
2 2013-3-6 1
2 2013-3-7 1
2 2013-3-8 1
2 2013-3-9 1
2 2013-3-11 1
2 2013-3-12 1
2 2013-3-13 1
2 2013-3-15
2 2013-3-16
2 2013-3-18 1
2 2013-3-19 1
2 2013-3-20 1
3 2013-3-1
3 2013-3-2
3 2013-3-4 1
3 2013-3-5 1
3 2013-3-6 1
3 2013-3-8
3 2013-3-9
3 2013-3-12 1
3 2013-3-13 1
3 2013-3-14 1
3 2013-3-15 1
3 2013-3-18 1
3 2013-3-19 1
3 2013-3-20 1


我的方法:

CREATE TABLE #t (eid INT,	DATE DATETIME, flag INT )
GO

INSERT #t(eid,DATE,flag)
select 1,'2013-3-1',null union 
select 1,'2013-3-2',null union 
select 1,'2013-3-4',null union 
select 1,'2013-3-5',null union 
select 1,'2013-3-7',1 union 
select 1,'2013-3-8',1 union 
select 1,'2013-3-9',1 union 
select 1,'2013-3-11',null union 
select 1,'2013-3-12',null union 
select 1,'2013-3-14',1 union 
select 1,'2013-3-15',1 union 
select 1,'2013-3-16',1 union 
select 1,'2013-3-18',null union 
select 2,'2013-3-1',null union 
select 2,'2013-3-2',null union 
select 2,'2013-3-4',null union 
select 2,'2013-3-6',1 union 
select 2,'2013-3-7',1 union 
select 2,'2013-3-8',1 union 
select 2,'2013-3-9',1 union 
select 2,'2013-3-11',1 union 
select 2,'2013-3-12',1 union 
select 2,'2013-3-13',1 union 
select 2,'2013-3-15',null union 
select 2,'2013-3-16',null union 
select 2,'2013-3-18',1 union 
select 2,'2013-3-19',1 union 
select 2,'2013-3-20',1 union 
select 3,'2013-3-1',null union 
select 3,'2013-3-2',null union 
select 3,'2013-3-4',1 union 
select 3,'2013-3-5',1 union 
select 3,'2013-3-6',1 union 
select 3,'2013-3-8',null union 
select 3,'2013-3-9',null union 
select 3,'2013-3-12',1 union 
select 3,'2013-3-13',1 union 
select 3,'2013-3-14',1 union 
select 3,'2013-3-15',1 union 
select 3,'2013-3-18',1 union 
select 3,'2013-3-19',1 union 
select 3,'2013-3-20',1  
go

select eid,
       date,
       flag,
       
       case when count(*) over(partition by eid,DATEadd(day,-rownum,date)) >=3
                 then 1
            else null
       end '计算出来的flag值'  -- 这列和你插入到flag中的值是一样的
from
(
select *,
       ROW_NUMBER() over(partition by eid order by date) rownum
from #t
)t
/*
eid	date	flag	计算出来的flag值
1	2013-03-01 00:00:00.000	NULL	NULL
1	2013-03-02 00:00:00.000	NULL	NULL
1	2013-03-04 00:00:00.000	NULL	NULL
1	2013-03-05 00:00:00.000	NULL	NULL
1	2013-03-07 00:00:00.000	1	1
1	2013-03-08 00:00:00.000	1	1
1	2013-03-09 00:00:00.000	1	1
1	2013-03-11 00:00:00.000	NULL	NULL
1	2013-03-12 00:00:00.000	NULL	NULL
1	2013-03-14 00:00:00.000	1	1
1	2013-03-15 00:00:00.000	1	1
1	2013-03-16 00:00:00.000	1	1
1	2013-03-18 00:00:00.000	NULL	NULL
2	2013-03-01 00:00:00.000	NULL	NULL
2	2013-03-02 00:00:00.000	NULL	NULL
2	2013-03-04 00:00:00.000	NULL	NULL
2	2013-03-06 00:00:00.000	1	1
2	2013-03-07 00:00:00.000	1	1
2	2013-03-08 00:00:00.000	1	1
2	2013-03-09 00:00:00.000	1	1
2	2013-03-11 00:00:00.000	1	1
2	2013-03-12 00:00:00.000	1	1
2	2013-03-13 00:00:00.000	1	1
2	2013-03-15 00:00:00.000	NULL	NULL
2	2013-03-16 00:00:00.000	NULL	NULL
2	2013-03-18 00:00:00.000	1	1
2	2013-03-19 00:00:00.000	1	1
2	2013-03-20 00:00:00.000	1	1
3	2013-03-01 00:00:00.000	NULL	NULL
3	2013-03-02 00:00:00.000	NULL	NULL
3	2013-03-04 00:00:00.000	1	1
3	2013-03-05 00:00:00.000	1	1
3	2013-03-06 00:00:00.000	1	1
3	2013-03-08 00:00:00.000	NULL	NULL
3	2013-03-09 00:00:00.000	NULL	NULL
3	2013-03-12 00:00:00.000	1	1
3	2013-03-13 00:00:00.000	1	1
3	2013-03-14 00:00:00.000	1	1
3	2013-03-15 00:00:00.000	1	1
3	2013-03-18 00:00:00.000	1	1
3	2013-03-19 00:00:00.000	1	1
3	2013-03-20 00:00:00.000	1	1
*/

这句SQL语句的意思是将一个变量@row_number加1,并将其命名为orderNum。在这个语句,@row_number是一个变量,通过赋操作符":="来给它赋。在这个例子,@row_number的初始是1,每执行这个语句,它的都会自增1。然后,通过AS子句,将其命名为orderNum。这样,每执行这个语句,都会生成一个新的序号,并以orderNum的形式返回。这个序号通常用于给查询结果排序或标记行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SQL Server Update字段ROW_NUMBER()](https://blog.csdn.net/tiemufeng1122/article/details/51202611)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [sql 游标、存储过程](https://download.csdn.net/download/longchanghua_enshi/7102383)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Sql 四大排名函数ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介](https://blog.csdn.net/qq_27997957/article/details/82532957)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值