在论坛中出现的比较难的sql问题:29(row_number函数 组内某列的值连续出现3次标记出来)
原文:在论坛中出现的比较难的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问题:29(row_number函数 组内某列的值连续出现3次标记出来)的更多相关文章
- 在论坛中出现的比较难的sql问题:27(字符串拆分、字符串合并、非连续数字的间隔范围、随机返回字符串)
原文:在论坛中出现的比较难的sql问题:27(字符串拆分.字符串合并.非连续数字的间隔范围.随机返回字符串) 在论坛中看到一个帖子,帖子中有一些sql方面的面试题,我觉得这些面试题很有代表性. 原帖的 ...
- 在论坛中出现的比较难的sql问题:7(子查询 判断某个字段的值是否连续)
原文:在论坛中出现的比较难的sql问题:7(子查询 判断某个字段的值是否连续) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以 ...
- 在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题)
原文:在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...
- 在论坛中出现的比较难的sql问题:45(用户在线登陆时间的小时、分钟计算问题)
原文:在论坛中出现的比较难的sql问题:45(用户在线登陆时间的小时.分钟计算问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. ...
- 在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据)
原文:在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决 ...
- 在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列)
原文:在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- 在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字)
原文:在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- 在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存)
原文:在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...
- 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)
原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...
随机推荐
- EnvironmentError: mysql_config not found
Collecting MySQL-python==1.2.5 (from -r requirementsNoGit.txt (line 9)) Using cached https://files.p ...
- python监控rabbitmq的消息队列数量
[root@localhost chen]# cat b.py #!/usr/bin/python # -*- coding: UTF-8 -*- import json,time import re ...
- vue的vuex在使用...mapState 和...mapGetter报错的解决方案
from:https://blog.csdn.net/ysterling/article/details/88145765 采用mapState 和mapGetter,在页面使用时报错,这是因为bab ...
- 【转】暴力破解无线WiFi密码
# coding:utf-8 import pywifi from pywifi import const import time from asyncio.tasks import sleep cl ...
- 十一、postman接口测试(安装nodejs和npm)
cmder安装:https://cmder.net/ node安装:https://nodejs.org/zh-cn/ 打开cmd命令,在命令提示窗输入 npm install -g cnpm --r ...
- 007-guava 缓存
一.概述 Guava Cache与ConcurrentMap很相似,但也不完全一样.最基本的区别是ConcurrentMap会一直保存所有添加的元素,直到显式地移除.相对地,Guava Cache为了 ...
- Qt获取时间戳作为图片名
Qt获取时间戳作为图片名 //保存图片 void SaveRealsenseImg() { QString picIndexName = dataSavePath; picIndexName.appe ...
- 【Java】分布式自增ID算法---雪花算法 (snowflake,Java版)
一般情况,实现全局唯一ID,有三种方案,分别是通过中间件方式.UUID.雪花算法. 方案一,通过中间件方式,可以是把数据库或者redis缓存作为媒介,从中间件获取ID.这种呢,优点是可以体现全局的递增 ...
- 123457123457#0#----com.MC.konglongtianse222----前拼后广--恐龙填色mc-mc1111
com.MC.konglongtianse222----前拼后广--恐龙填色mc
- mysql quick query row count using sql
1. command show table status like '{table-name}'; 2. sample mysql> use inventory; Database change ...
