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 7501325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:29:29+00:00 2026-05-29T20:29:29+00:00

We have a table with status updates for subscriptions to a product. A record

  • 0

We have a table with status updates for subscriptions to a product. A record is inserted into the table when the subscription begins, and that record is updated with an end date when the subscription ends. One of our systems (no idea which one) sometimes does a “same day drop\add” where it ends the subscription and then begins it again (creating a new record). So the same subscriber ID is attached to multiple records, even though nothing really changed.

Example data would be this:

recID subID   start           end        prodtype
1     19    01/11/2001  01/15/2001    A
2     19    01/15/2001  01/16/2001    A
3     19    01/16/2001  01/20/2001    A
4     19    01/30/2001  01/31/2001    A

This guy started on 1/11 and ended on 1/20. Records 2 and 3 were put in by the system (same day drop add, but weren’t really). Record 4 is another subscription Mr. 19 started later.

I have some code that will attempt to resolve only the first (the real) record of each distinct subscription, but it can’t find the real end date without using max() and grouping by the subscriber. That of course would show two subscriptions, 1/11 – 1/31 and 1/30 – 1/31, which is wrong.

I’m tearing my hair out trying to resolve this pattern down to two records like this:

subID   start           end        prodtype
 19    01/11/2001   01/20/2001    A
 19    01/30/2001   01/31/2001    A

This is in Teradata, but its just ANSI SQL, I believe.

  • 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-05-29T20:29:30+00:00Added an answer on May 29, 2026 at 8:29 pm

    I believe this is ANSI SQL, but I’ve only tested it on SQL Server.

    Basically, the query is able to find true start dates and true end dates independently of each other. Then to associate the start date and end dates, associates start dates with end dates that are greater than the start date… and then shows the smallest end date.

    SELECT
        startDates.subId,
        startDates.startDate,
        MIN(endDates.endDate) AS endDate,
        startDates.prodType
    FROM
    (
        SELECT
            recID, subID, startDate, prodType
        FROM yourTable s1
        WHERE NOT EXISTS (
            SELECT 1
            FROM yourTable s2
            WHERE 
                s1.startDate = s2.endDate
                AND s1.subId = s2.subId
        )
    ) startDates JOIN
    (
        SELECT
            recID, subID, endDate, prodType
        FROM yourTable s1
        WHERE NOT EXISTS (
            SELECT 1
            FROM yourTable s2
            WHERE 
                s1.endDate = s2.startDate
                AND s1.subId = s2.subId
        )
    ) endDates ON
        startDates.subID = endDates.subID 
        AND startDates.startDate < endDates.endDate
    GROUP BY
        startDates.subId,
        startDates.startDate,
        startDates.prodType
    

    Here is the query in action…

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

Sidebar

Related Questions

I have a function that updates a MySQL table from a CSV file. The
I have a query that updates a boolean value for a given record, then
I have two table emptable1(empid,status) emptable2(empid,week) i want to select all the empid whose
I have a table which contains my server status create table ServerStatus ( ServerId
I have a table ClassAttendance and I'm using MSSQL 2005 studentID attendanceDate status -------------------------------------
I have a table with three columns: user varchar, status varchar , rep int
I have a table called ApprovalTasks... Approvals has a status column I also have
I have a MySQL InnoDB table with a status column. The status can be
I need to update existing users IsApproved status in the aspnet_Membership table. I have
I have table [HTML Table] on page I want to delete record from table

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.