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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:48:28+00:00 2026-06-13T07:48:28+00:00

So I have the following membership history table User_ID | Start date | End

  • 0

So I have the following membership history table

User_ID  |  Start date  |  End Date  |  Type(0-7) |  
--------------------------------------------------
    2    |   date1      |  date2     |    0       |  
--------------------------------------------------
    2    |   date3      |  date4     |    3       |  
--------------------------------------------------
    2    |   date5      |  date6     |    3       |  
--------------------------------------------------
    2    |   date7      |  date8     |    1       |  
--------------------------------------------------
    3    |   date9      |  date10    |    6       |  
--------------------------------------------------
    3    |   date11     |  date12    |    0       |  
--------------------------------------------------

I am trying to create the mysql query with the result:

    User_ID  |  Start date  |  End Date  |  Type(0-7) |PrevEnd-CurStart| Prevtype    
    ----------------------------------------------------------------------------------
        2    |   date1      |  date2     |    0       |  null OR 0     | null OR 0
    ----------------------------------------------------------------------------------
        2    |   date3      |  date4     |    3       |  Date3-Date2   |  0
    ----------------------------------------------------------------------------------
        2    |   date5      |  date6     |    3       |  Date5-Date4   |  3
    ----------------------------------------------------------------------------------
        2    |   date7      |  date8     |    1       |  Date7-Date6   |  3
    ----------------------------------------------------------------------------------
        3    |   date9      |  date10    |    6       |  null Or 0     |  null OR 0
    ----------------------------------------------------------------------------------
        3    |   date11     |  date12    |    0       |  Date11-Date10 |  6
    ----------------------------------------------------------------------------------

I put in Null or 0 because I would be able to work with it either way from that point on

How would I go about writing the mysql query for that? this is just a sub-query for a report I need, and I’ve been stuck on it all day yesterday, last night and today. Would really appreciate the help. Run time has little relevance, as I need to run this just once per month.

EDIT:
To get my current output, I use the following. The ‘group by 1,2,3,4’ is because of a lot of duplicates that I need to get rid of

select 
 mh.account_id, 
 mh.start_date, 
 mh.end_date, 
 mh.`type`
from tbl_membership_history mh join tbl_membership m on mh.account_id=m.account_id
group by 1,2,3,4
order by 1,2,3,4
limit 100
  • 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-13T07:48:29+00:00Added an answer on June 13, 2026 at 7:48 am

    You can make a self-join on your table with earlier records, then aggregate to find the latest of those earlier records’ end dates (which gives the previous end). One must then join that result to the table a further time in order to find the Type of the previous record:

    SELECT   res.*, prv.Type AS prvType
    FROM (
      SELECT   cur.*, MAX(bef.endDate) AS prvEnd
      FROM     history AS cur LEFT JOIN history AS bef
            ON bef.User_ID = cur.User_ID AND bef.endDate <= cur.startDate
      GROUP BY cur.User_ID, cur.startDate, cur.endDate, cur.Type
    ) res LEFT JOIN history AS prv
            ON prv.User_ID = res.User_ID AND prv.endDate = res.prvEnd
    

    See it on sqlfiddle.

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

Sidebar

Related Questions

I have the following table in my database Membership ComapanyId IsMember JoinedDate ExpiryDate RejoinDate
I have following CSS: table tbody tr:last-child td { padding-top: 7px; border-bottom: 0; }
I have following SQL statementL: select DATE(bla), count(*) from tableA group by DATE(bla) UNION
I have the following ASP.NET Membership section defined in the Web.config file: <membership defaultProvider=AspNetActiveDirectoryMembershipProvider>
I have the following tables: CREATE TABLE `attendance_event_attendance` ( `id` int(11) NOT NULL AUTO_INCREMENT,
I have following HTML markup, <div id=subcontent_l> <p> <a href=/membership-packages/><img height=202 width=644 alt= src=http://74.52.72.231/wp-content/uploads/2010/06/banner1.jpg
I have the following class: public class Membership { public DateTime StartDate { get;
i have the following code: if (Membership.FindUsersByName(username) == null) { Membership.CreateUser(username, password, email); }
I have the following line in a Controller ViewData[UsersOnLineNow] = Membership.GetNumberOfUsersOnline().ToString(); I am logged
I have the following DIV markup: <div id=dialog title=Membership Renewal> Your membership is going

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.