Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the next occurrence of a value in a table

Tags:

sql

tsql

Sorry in advance if this has already been covered.

I am working on a database which isnt particularly well structured but it is owned by a third party and cannot be changed.

I need some assistance with t-sql in find the next occurrence of a value within the table and return records based on the result. Let me first explain the data. I have simplified this to make it easier to understand.

Polref      Effective Date       Transaction Type     Suffix        Value
ABCD1       01/06/2010           New Bus              1             175.00
ABCD1       01/06/2011           Ren                  2             200.00
ABCD1       19/08/2011           Adjust               3              50.00
ABCD1       23/04/2012           Adjust               4              50.00
ABCD1       01/06/2012           Ren                  5             275.00

So if I ran my query for 2011, the code would need to return in this example rows with suffix 2,3 and 4. So what I have been trying to do is find the first suffix of a New Bus or Ren for the specified year and then finding the next suffix for a New Bus or Ren for the same polref and then using those two suffix values to limit my recordset. It aint working!!

I cant use MAX() as transactions for 2013 have already been added to the system to I would get more records than I actually need.

There result I should be expecting for this example data would be:

ABCD1 300.00

Any help would be greatly appreciated.

To answer another question, If I select 2011 as my year to run the report, there should only be one New Bus or Ren transaction for 2011 so if its a New Bus transaction, the next main transaction will be a Ren, if its a Ren then the next main transaction will be a Ren. Again in my example below, if I run for 2011, it should find the Ren from 01/06/2011 so I want to return that Ren and the two Adjust records.

Sorry, I've not used this forum before so apologies if I was a little vague.

The table I am using has many polrefs so I need this code to calculate totals for all polrefs that fall within the date range. Some polrefs may only have one row, a New Bus, some will have many rows depending on how many adjustments have been made throughout the year of the policy

like image 942
Julian Slaughter Avatar asked Nov 04 '22 12:11

Julian Slaughter


1 Answers

Partial answer:

This query:

declare @t table (PolRef char(5) not null, EffectiveDate date not null,TransactionType varchar(10) not null,Suffix int not null,Value decimal(10,2) not null)
insert into @t (Polref,EffectiveDate,TransactionType,Suffix,Value) values
('ABCD1','20100601','New Bus',1,175.00),
('ABCD1','20110601','Ren',2,200.00),
('ABCD1','20110819','Adjust',3,50.00),
('ABCD1','20120423','Adjust',4,50.00),
('ABCD1','20120601','Ren',5,275.00)

;With StartTransactions as (
    select PolRef,Suffix,ROW_NUMBER() OVER (PARTITION BY PolRef ORDER BY Suffix) rn
    from @t where TransactionType in ('New Bus','Ren')
), Periods as (
    select st1.PolRef,st1.Suffix as StartSuffix,st2.Suffix as EndSuffix
    from
        StartTransactions st1
        left join
        StartTransactions st2
            on
                st1.PolRef = st2.PolRef and
                st1.rn = st2.rn - 1
)
select
    p.PolRef,t2.EffectiveDate,SUM(t.Value) as Total
from
    Periods p
        inner join
    @t t
        on
            p.PolRef = t.PolRef and
            p.StartSuffix <= t.Suffix and
            (p.EndSuffix > t.Suffix or
            p.EndSuffix is null)
        inner join
    @t t2
        on
            p.PolRef = t2.PolRef and
            t2.Suffix = p.StartSuffix
group by
    p.PolRef,t2.EffectiveDate

Groups each set of transactions based on each successive Ren or New Bus transaction:

PolRef EffectiveDate Total
------ ------------- ---------------------------------------
ABCD1  2010-06-01    175.00
ABCD1  2011-06-01    300.00
ABCD1  2012-06-01    275.00

From that, it should be trivial to e.g. select out only the ones you're interested in from a particular year. But your question is still vague on some specifics, so I'm not taking it any further at this point.

like image 68
Damien_The_Unbeliever Avatar answered Nov 08 '22 05:11

Damien_The_Unbeliever