Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8762433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:31:47+00:00 2026-06-13T15:31:47+00:00

Sorry in advance if this has already been covered. I am working on a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:31:48+00:00Added an answer on June 13, 2026 at 3:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi and sorry advance if this has already been covered. I'm mainly preoccupied with
Sorry if this question has already been answered but I can't find an answer.
Sorry If something like this has already been asked, but I can't find exactly
I am sorry, for I believe that this question has been asked already, but
Sorry if this has been asked before but I've tried searching and can't find
Sorry if this has been asked many times before, but nothing seemed to help
and sorry in advance if this question has been solved previously. I am creating
Thanks in advance, and sorry if this question has been answered previously - I
I'm sorry if this question has been asked. I have looked but can not
Sorry if something similar has been posted here already, but I couldn't really find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.