salve,
frankie64 wrote:
Ciao a tutti.
Ho una tabella-database così costituita:
IdValue
1 0
2 0
3 1
4 1
5 1
6 0
7 0
8 0
9 1
10 0
11 1
12 0
Secondo voi, desiderando una select che mi restituisca come valore il numero max degli zero consecutivi ( in tabella la serie di valori zero max consecutivi è 3) e il numero max degli 1 consecutivi ( anche in questo caso è pari 3), come bisognerebbe agire?
Grazie a chi vorrà rispondere.
frankie
visto che mi sembrava una cosa interessante per lui, ho scomodato un caro amico genialmente matematico... e la sua soluzione e', ovviamente, lapalissiana...
declare @T table(Id int identity primary key, V tinyint)
insert into @T values(0),(0),(1),(1),(1),(0),(0),(0),(1),(0),(1),(0),(0)
select t.V, C=max(c.C)
from @T t
outer apply(
select top 1 *
from @T n
where n.Id>t.Id and n.V!=t.V
) n
outer apply(
select C=count(*)
from @T c
where c.Id>=t.Id and (c.Id<n.Id or n.Id is null)
) c
group by t.V
Marcello Poletti
Dingo&Epomops
http://www.dingoepomops.ithttp://blogs.dotnethell.it/epomops/saluti a tutti