Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
andrea640
Starting Member
5 Posts |
Posted - 2011-08-06 : 21:39:49
|
Hello everyone.I have a table-database consists of two columns:Id* Value 1* 02* 03* 14* 15* 16* 07* 08* 09* 110* 011* 112* 0According to you, wanting me to return as a select valueThe maximum number of consecutive zero (in the table the maximum number of consecutive zeros is 3) and the maximum number of consecutive 1 (in this case is 3), how to act?Thanks to anyone who wishes to respond.frankie |
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-08-07 : 02:33:18
|
Found a similar question and answer on another forum posted by Brian Swan. Made a couple modifications. I believe this should get you what you want:declare @table table (id int primary key identity not null, intvalue tinyint)insert into @table (intvalue)values (0), (0), (1), (1), (1), (0), (0), (0), (1), (0), (1), (0)--get the number of rowsdeclare @row_count as int;set @row_count= (select COUNT(*) from @table);--a counter to be used in the while loopdeclare @count as int;set @count = 1;--keep track of the highest number of consecutive zerosdeclare @high_zero_count as int;set @high_zero_count=0;--keep track of consecutive zerosdeclare @zero_count as int;set @zero_count=0;--set starting datedeclare @id int;set @id = 1while @count <= @row_countbegin if (select intvalue from @table where id = @id) = 1 begin set @zero_count = 0; end else begin set @zero_count = @zero_count + 1; end if (@zero_count > @high_zero_count) begin set @high_zero_count = @zero_count; end --advance id by one set @id=@id+1; --increment the counter set @count = @count + 1;end;select @high_zero_count; |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-08-07 : 03:41:37
|
[code]; withgrp as( select *, grp = id - row_number() over (order by value, id) from tbl -- yourtable)select top 1 count(*)from grpwhere value = 0 -- change to 1 for finding consecutive 1group by grporder by count(*) desc[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
andrea640
Starting Member
5 Posts |
Posted - 2011-08-07 : 04:32:50
|
Thanks for the replies: much. |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-08-07 : 11:04:34
|
Sure thing..Khtan's example is WAY more practical...that's actually going to help me with something else I need to do.Khtan, any idea how you would handle it if you wanted to group by each value to return the max rows of each, not just 0?Doing a group by intvalue skews the results. So say you wanted to returnintvalue MaxNumberRows0 31 3 |
|
|
andrea640
Starting Member
5 Posts |
Posted - 2011-08-09 : 18:24:29
|
Thanks: marvellous |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-08-09 : 20:39:54
|
[code]; withgrp as( select *, grp = id - row_number() over (order by value, id) from tbl),grp2 as( select value, cnt = count(*) from grp group by grp, value)select value, max(cnt)from grp2group by value[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-08-14 : 15:44:42
|
Ah, very cool...I'm following :) thanks khtan |
|
|
|
|
|
|
|