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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:09:36+00:00 2026-06-16T20:09:36+00:00

I have a table which lists every date within the year. Each row also

  • 0

I have a table which lists every date within the year. Each row also contains a period number from 1 to 13 which loop around each year.
It looks something like this:

|        Date         | Period |
| 2012-12-27 00:00:00 |   12   |
| 2012-12-28 00:00:00 |   12   |
| 2012-12-29 00:00:00 |   13   |
| 2012-12-30 00:00:00 |   13   |
| 2012-12-31 00:00:00 |   13   |
| 2013-01-01 00:00:00 |   13   |
| 2013-01-02 00:00:00 |   13   |
|        . . .        |   13   | 
| 2013-02-10 00:00:00 |   01   |
|        . . .        |   01   | 
| 2013-03-14 00:00:00 |   02   |
|        . . .        |   02   | 
| 2013-05-05 00:00:00 |   03   |

As you can see above, period 13 starts on the 29th of dec 2012 and finishes on the 09th of Febuary 2013.
I work this out by getting the first and last dates for period 13.

I need to write a query that will get the period start and end dates for the current period.
But I’m having problems because the periods sometimes occur twice a year and sometimes overlap between the years like this example does.

A few more examples:

If getDate() = ‘2013-02-25 13:45:00’ then the period should be 01 and the start and end dates would be ‘2013-02-10 00:00:00’ and ‘2013-03-14 00:00:00’

If getDate() = ‘2013-03-15 00:00:00’ then the period should be 02 and the start and end dates would be ‘2013-03-14 00:00:00’ and ‘2013-05-05 00:00:00’

I really hope this makes sense. Let me know if I need to clarify it! Thanks for your help 🙂

  • 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-16T20:09:38+00:00Added an answer on June 16, 2026 at 8:09 pm

    If you were using SQL Server 2012, you would use lag or lead for this. Instead, I’m going to use a correlated subquery:

    select min(period) as period, MIN(date), MAX(date)
    from (select t.*,
                 (select min(date) from t t2 where t2.period <> t.period and t2.date > t.date) as nextp
          from t
         ) t
    group by nextp
    

    The inner subquery gets the date of the next period. This is the same for consecutive records with the same period record. I can then use this for grouping.

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

Sidebar

Related Questions

i have a table which contains a bunch of dynamically created radio button lists,
I have a three column table which lists every person, every event they may
I have a table named tbl_Subjects_Taken which lists all the subjects taken by the
I have a lookup table called metadata_types in my database, which lists all the
I have a table which contains a list of Surveys (PK is ID) CREATE
I have a table which contains a list of products for a company. They
I have a Posts model and every post also contains Blocks (also a model).
I have a members table in which every site member is the unique id.
I have a table which grows dynamically with images when I select images from
I have a table which holds a list of criminal charges.These charges can be

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.