Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Group Consecutive Club Membership Dates by Member and Membership Type

$
0
0
I have a database table that currently holds membership joining information (extract below) with one line for each year/period that a 
member have paid for membership; It is possible for a member to cancel their membership for one year/period and rejoin at a later date. 
We need to represent each continuous membership as one line such that contents of Extract 1 below becomes Extract 2:

Extract 1

Member_No   GROUPTypeStart_DateEnd_Date
---------   ------  --------- --------------------
10       MEMJUNIOR2010-01-01 00:00:00.000    2011-01-01 00:00:00.000
10       MEMJUNIOR2011-01-01 00:00:00.0002012-01-01 00:00:00.000
10       MEMJUNIOR2012-01-01 00:00:00.000    2013-01-01 00:00:00.000
20       MEMJUNIOR2005-01-01 00:00:00.000    2006-01-01 00:00:00.000
20       MEMSENIOR2006-01-01 00:00:00.000    2007-01-01 00:00:00.000
20       MEMSENIOR2007-01-01 00:00:00.000    2008-01-01 00:00:00.000
20MEM    SENIOR2008-01-01 00:00:00.000    2009-01-01 00:00:00.000
30  MEM    SENIOR2005-01-01 00:00:00.000    2006-01-01 00:00:00.000
30  MEM    SENIOR 2006-01-01 00:00:00.000    2007-01-01 00:00:00.000
30  MEM    SENIOR2007-01-01 00:00:00.000    2008-01-01 00:00:00.000
30  MEM    SENIOR2008-01-01 00:00:00.000    2009-01-01 00:00:00.000
30  MEM    SENIOR2009-01-01 00:00:00.000    2010-01-01 00:00:00.000
30  MEM    SENIOR2013-01-01 00:00:00.000    2014-01-01 00:00:00.000
Extract 2

Member_No   GROUPTypeStart_Date               End_Date
---------   -------   ------------------------
10       MEMJUNIOR2010-01-01 00:00:00.000    2013-01-01 00:00:00.000
20       MEMJUNIOR2005-01-01 00:00:00.000    2006-01-01 00:00:00.000
20       MEMSENIOR2006-01-01 00:00:00.000    2009-01-01 00:00:00.000
30       MEMSENIOR2005-01-01 00:00:00.000    2010-01-01 00:00:00.000
30       MEMSENIOR2013-10-13 00:00:00.000    2014-01-01 00:00:00.000

I have the query below but this does quite work as I expect it to - any help would be greatly appreciated:

;with ms as (
      select Member_No, Type, valid_from, valid_to,
             (	select 1 
				from Membership ms2 
				where ms2.Member_No = ms.Member_No 
				and cast(ms2.valid_From as date) = cast(ms.valid_to as date)
				and Type in ('mem')
             ) as LinkedToNext
      from Membership ms where activity = 'mem'
     )
select Member_No, Type, MIN(valid_from) as start_date, MAX(valid_to) as end_date
from (select ms.*,
             (select top 1 valid_to
              from ms ms2 where ms2.Member_No = ms.Member_No and 
              ms2.LinkedToNext is NULL and ms2.valid_from >= ms.valid_to
             order by Type, valid_to desc
             ) as grouping
      from ms
     ) ms1
group by Member_No, Type, grouping
order by Member_No, start_date



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>