views:

74

answers:

1

Hello,

I have a table like that

   ID    ORDER  TEAM   TIME
   IL-1   1   A_Team   11
   IL-1   2   A_Team   3
   IL-1   3   B_Team   2
   IL-1   4   A_Team   1
   IL-1   5   A_Team   1
   IL-2   1   A_Team   5
   IL-2   2   C_Team   3

What I want is grouping the same named teams which are also sequential teams (Which is according to the ORDER column)

So the result table should look like

IL-1  1  A_Team 14
IL-1  2  B_Team 2
IL-1  3  A_Team 2
IL-2  1  A_Team 5
IL-2  2  C_Team 3

Thanks

Edit: Depending on the nang's answer, I added the ID column to my table.

A: 

There is a problem in your example. Why should rows #6 and #2 not be "sequential teams"?

1 A_Team 5

2 A_Team 3

However, maybe the following is usefull for you:

select neworder, name, sum([time]) from  (
select min(n1.[order]) neworder, n2.[order], n2.name, n2.[time]
from mytable n1, mytable n2
where n1.Name = n2.Name
and n2.[order] >= n1.[order]
and not exists(select 1 from mytable n3 where n3.name != n1.name and n3.[order] > n1.[order] and n3.[order] < n2.[order])
group by n2.[order], n2.name, n2.[time]) x
group by neworder, name

Result:

neworder name (No column name)

1 A_Team 19

4 A_Team 2

3 B_Team 2

2 C_Team 3

nang
For the problem you mentioned about sequential teams, I edited my question and add ID field to my table, which I didn't write before to make the table look more simple while asking.
stckvrflw