+ -
当前位置:首页 → 问答吧 → 求一SQL统计代码

求一SQL统计代码

时间:2011-11-25

来源:互联网

原始表如下:
ID 品系 停割
1 600 0
2 600 0
3 600 1
4 600 1
5 525 1
6 525 0
7 525 0
8 525 0
9 525 1
10 525 1
11 525 0

表中1表示停割,0代表未停割,现要统计连续停割的数目,希望输出结果:
ID 品系 停割 统计值
1 600 0 0
2 600 0 0
3 600 1 3
4 600 1 3
5 525 1 3
6 525 0 0
7 525 0 0
8 525 0 0
9 525 1 2
10 525 1 2
11 525 0 0

几年不写代码了,还没怎么有头绪,请高手指点!

作者: chbq1210   发布时间: 2011-11-25

SQL code
--> --> (Roy)生成測試數據
 
if not object_id('Tempdb..#T') is null
    drop table #T
Go
Create table #T([ID] int,[品系] int,[停割] int)
Insert #T
select 1,600,0 union all
select 2,600,0 union all
select 3,600,1 union all
select 4,600,1 union all
select 5,525,1 union all
select 6,525,0 union all
select 7,525,0 union all
select 8,525,0 union all
select 9,525,1 union all
select 10,525,1 union all
select 11,525,0
Go
;with b
as
(Select *,row=[ID]-ROW_NUMBER()over(partition by [停割] order by ID)
from #T as a) 

select 
a.*,统计值=isnull(b.con,0)
from #T as a
left join 
(select min(ID) as MinID,Max(ID) as MaxID,count(*) as con,[停割] from b  group by row,[停割] ) as b 
on  a.ID between b.MinID and b.MaxID and b.[停割]=1
/*
ID    品系    停割    统计值
1    600    0    0
2    600    0    0
3    600    1    3
4    600    1    3
5    525    1    3
6    525    0    0
7    525    0    0
8    525    0    0
9    525    1    2
10    525    1    2
11    525    0    0
*/

作者: roy_88   发布时间: 2011-11-25

谢谢楼上的朋友,确实历害,好几个关键词如WITH,OVER我之前都没有用过,代码得好好的分析一下。
另外,如果ID不连续的话,貌似这个方法还是行不能哦,比如把表变为:
CREATE TABLE tbl_temp([ID] INT,[品系] INT,[停割] INT)
INSERT tbl_temp
SELECT 10,600,0 UNION ALL
SELECT 21,600,0 UNION ALL
SELECT 38,600,1 UNION ALL
SELECT 42,600,1 UNION ALL
SELECT 56,525,1 UNION ALL
SELECT 65,525,0 UNION ALL
SELECT 73,525,0 UNION ALL
SELECT 80,525,0 UNION ALL
SELECT 98,525,1 UNION ALL
SELECT 109,525,1 UNION ALL
SELECT 110,525,0

作者: chbq1210   发布时间: 2011-11-25

SQL code

Create table #T([ID] int,[品系] int,[停割] int)
Insert #T
select 1,600,0 union all
select 2,600,0 union all
select 3,600,1 union all
select 4,600,1 union all
select 5,525,1 union all
select 6,525,0 union all
select 7,525,0 union all
select 8,525,0 union all
select 9,525,1 union all
select 10,525,1 union all
select 11,525,0
Go
;with b
as
(Select *,row=ROW_NUMBER() over (order by getdate())-ROW_NUMBER()over(partition by [停割] order by ID)
from #T as a) 

select 
a.*,统计值=isnull(b.con,0)
from #T as a
left join 
(select min(ID) as MinID,Max(ID) as MaxID,count(*) as con,[停割] from b  group by row,[停割] ) as b 
on  a.ID between b.MinID and b.MaxID and b.[停割]=1

drop table #T

/*****************

ID          品系          停割          统计值
----------- ----------- ----------- -----------
1           600         0           0
2           600         0           0
3           600         1           3
4           600         1           3
5           525         1           3
6           525         0           0
7           525         0           0
8           525         0           0
9           525         1           2
10          525         1           2
11          525         0           0

(11 行受影响)

作者: AcHerat   发布时间: 2011-11-25