Alec.
Alec.

Reputation: 5525

TSQL - Difficult Grouping

Please see fiddle: http://sqlfiddle.com/#!6/e6768/2

I have data, like below:

DRIVER  DROP
1       1
1       2
1       ReturnToBase
1       4
1       5
1       ReturnToBase
1       6
1       7    
2       1
2       2
2       ReturnToBase
2       4

I am trying to group my data, so for each driver, each group of return to bases have a grouping number.

My output should look like this:

DRIVER      DROP           GROUP
1            1              1
1            2              1
1            ReturnToBase   1
1            4              2
1            5              2
1            ReturnToBase   2
1            6              3
1            7              3
1            ReturnToBase   3               
2            1              1           
2            2              1
2            ReturnToBase   1   
2            4              2

I've tried getting this result with a combination of windowed functions but I've been miles off so far

Below is what I had so far, it isn't supposed to be functional I was trying to figure out how it could be done, if it's even possible.

SELECT 
    ROW_NUMBER() OVER (Partition BY Driver order by Driver Desc) rownum,
    Count(1) OVER (Partition By Driver Order By Driver Desc) counter,
    Count
    DropNo,
    Driver,
    CASE DropNo 
       WHEN 'ReturnToBase' THEN 1 ELSE 0 END AS EnumerateRound
FROM 
    Rounds

Upvotes: 2

Views: 63

Answers (2)

Giorgos Betsos
Giorgos Betsos

Reputation: 72165

You can use the following query:

SELECT id, DRIVER, DROPno, 
       1 + SUM(flag) OVER (PARTITION BY DRIVER ORDER BY id) -
       CASE 
          WHEN DROPno = 'ReturnToBase' THEN 1 
          ELSE 0 
       END AS grp       
FROM (
  SELECT id, DRIVER, DROPno, 
         CASE 
            WHEN DROPno = 'ReturnToBase' THEN 1 
            ELSE 0 
         END AS flag
  FROM rounds ) AS t

Demo here

This query uses windowed version of SUM with ORDER BY in the OVER clause to calculate a running total. This version of SUM is available from SQL Server 2012 onwards AFAIK.

Fiddling a bit with this running total value is all we need in order to get the correct GROUP value.

EDIT: (credit goes to @Conrad Frix)

Using CROSS APPLY instead of an in-line view can considerably simplify things:

SELECT id, DRIVER, DROPno, 
       1 + SUM(x.flag) OVER (PARTITION BY DRIVER ORDER BY id) - x.flag
FROM rounds
CROSS APPLY (SELECT CASE WHEN DROPno = 'ReturnToBase' THEN 1 ELSE 0 END) AS x(flag)

Demo here

Upvotes: 5

Sam CD
Sam CD

Reputation: 2097

Added a sequential ID column to your example for use in a recursive CTE:

with cte as (
select ID,DRIVER,DROPno,1 as GRP
FROM rounds
where ID = 1
union all
select a.ID
,a.DRIVER
,a.DROPno
,case when b.DROPno = 'ReturnToBase' 
      or b.DRIVER <> a.DRIVER then b.GRP + 1 
      else b.GRP end
from rounds a
  inner join cte b
    on a.ID = b.ID + 1
)

select * from cte

SQL Fiddle

Upvotes: 2

Related Questions