您的位置:首页 > 数据库

在论坛中出现的比较难的sql问题:19(row_number函数3)

2014-01-02 14:58 453 查看
最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。

1、怎么用简单的sql语句记流水? http://bbs.csdn.net/topics/390674508
有一个流水表TF,字段有ID,FirstQuantity,ChangeQuantity,FinalQuantity。ID表示物品,后面几个表示期初数量,变化数量,最终数量。
假设表TF现在是空
有一张变动表TC,字段有ID,Quantity。表示某物品的数量。里面会有重复。内容如下:
ID Quantity
1 10
1 20
1 30
那么当我把TC的数据加入到TF后,TF的内容应该如下:
ID FirstQuantity ChangeQuantity FinalQuantity 1   0             10              10 1   10            20              30 1   30            30              60
这个功能,用编程的方法很好解决,就是一个一个循环写入,但是效率太慢了。
那么能不能用一条sql语句就搞定呢?

我的方法:
create table tc(ID int, Quantity int)  insert into tc select 1  ,10  union all select 1  ,20 union all select 1  ,30  go  ;with t as ( select *,        ROW_NUMBER() over(partition by id order by @@servername) rownum from tc )   select ID,        FirstQuantity,        ChangeQuantity,        FirstQuantity+ChangeQuantity as inalQuantity from  ( select ID,        case when rownum = 1 then 0              else (select SUM(Quantity) from t t2                    where t2.ID = t1.id and t2.rownum < t1.rownum)        end as FirstQuantity,        Quantity as ChangeQuantity        from t t1 )tt /* ID	FirstQuantity	ChangeQuantity	inalQuantity 1	0	10	10 1	10	20	30 1	30	30	60 */


2、组内某列的值连续出现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 */


本文出自 “探索SQLServer” 博客,请务必保留此出处http://yupeigu.blog.51cto.com/3323001/1367951
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐