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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:07:43+00:00 2026-05-15T16:07:43+00:00

My goal is to return a start and end date having same value in

  • 0

My goal is to return a start and end date having same value in a column. Here is my table. The (*) have been marked to give you the idea of how I want to get “EndDate” for every similar sequence value of A & B columns

ID | DayDate   |  A  |  B
-----------------------------------------------
1  | 2010/07/1 | 200 |  300
2  | 2010/07/2 | 200 |  300 *
3  | 2010/07/3 | 150 |  250
4  | 2010/07/4 | 150 |  250 *
8  | 2010/07/5 | 150 |  350 *
9  | 2010/07/6 | 200 |  300
10 | 2010/07/7 | 200 |  300 *
11 | 2010/07/8 | 100 |  200
12 | 2010/07/9 | 100 |  200 *

and I want to get the following result table from the above table

| DayDate   |EndDate   |  A  |  B
-----------------------------------------------
| 2010/07/1 |2010/07/2 | 200 |  300
| 2010/07/3 |2010/07/4 | 150 |  250
| 2010/07/5 |2010/07/5 | 150 |  350
| 2010/07/6 |2010/07/7 | 200 |  300
| 2010/07/8 |2010/07/9 | 100 |  200

UPDATE:

Thanks Mike, The approach of yours seems to work in your perspective of considering the following row as a mistake.

8  | 2010/07/5 | 150 |  350 * 

However it is not a mistake. The challenge I am faced with this type of data is like a scenario of logging a market price change with date. The real problem in mycase is to select all rows with the beginning and ending date if both A & B matches in all these rows. Also to select the rows which are next to previously selected, and so on like that no data is left out in the table.

I can explain a real world scenario. A Hotel with Room A and B has room rates for each day entered in to table as explained in my question. Now the hotel needs to get a report to show the price calendar in a shorter way using start and end date, instead of listing all the dates entered. For example, on 2010/07/01 to 2010/07/02 the price of A is 200 and B is 300. This price is changed from 3rd to 4th and on 5th there is a different price only for that day where the Room B is price is changed to 350. So this is considered as a single day difference, thats why start and end dates are same.

I hope this explained the scenario of the problem. Also note that this hotel may be closed for a specific time period, lets say this is an additional problem to my first question. The problem is what if the rate is not entered on specific dates, for example on Sundays the hotel do not sell these two rooms so they entered no price, meaning the row will not exist in the table.

  • 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-15T16:07:44+00:00Added an answer on May 15, 2026 at 4:07 pm

    Creating related tables allows you much greater freedom to query and extract relevant information. Here’s a few links that you might find useful:

    You could start with these tutorials:
    http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html
    http://net.tutsplus.com/tutorials/databases/sql-for-beginners/

    There are also a couple of questions here on stackoverflow that might be useful:
    Normalization in plain English
    What exactly does database normalization do?

    Anyway, on to a possible solution. The following examples use your hotel rooms analogy.

    First, create a table to hold information about the hotel rooms. This table just contains the room ID and its name, but you could store other information in here, such as the room type (single, double, twin), its view (ocean front, ocean view, city view, pool view), and so on:

    CREATE TABLE `room` (
      `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      `name` VARCHAR(45) NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE INDEX `name_UNIQUE` (`name` ASC) )
    ENGINE = InnoDB;
    

    Now create a table to hold the changing room rates. This table links to the room table through the room_id column. The foreign key constraint prevents records being inserted into the rate table which refer to rooms that do not exist:

    CREATE TABLE `rate` (
      `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
      `room_id` INT UNSIGNED NOT NULL,
      `date` DATE NOT NULL,
      `rate` DECIMAL(6,2) UNSIGNED NOT NULL,
      PRIMARY KEY (`id`),
      INDEX `fk_room_rate` (`room_id` ASC),
      CONSTRAINT `fk_room_rate`
        FOREIGN KEY (`room_id` )
        REFERENCES `room` (`id` )
        ON DELETE CASCADE
        ON UPDATE CASCADE)
    ENGINE = InnoDB;
    

    Create two rooms, and add some daily rate information about each room:

    INSERT INTO `room` (`id`, `name`) VALUES (1, 'A'), (2, 'B');
    
    INSERT INTO `rate` (`id`, `room_id`, `date`, `rate`) VALUES
    ( 1, 1, '2010-07-01', 200),
    ( 2, 1, '2010-07-02', 200),
    ( 3, 1, '2010-07-03', 150),
    ( 4, 1, '2010-07-04', 150),
    ( 5, 1, '2010-07-05', 150),
    ( 6, 1, '2010-07-06', 200),
    ( 7, 1, '2010-07-07', 200),
    ( 8, 1, '2010-07-08', 100),
    ( 9, 1, '2010-07-09', 100),
    (10, 2, '2010-07-01', 300),
    (11, 2, '2010-07-02', 300),
    (12, 2, '2010-07-03', 250),
    (13, 2, '2010-07-04', 250),
    (14, 2, '2010-07-05', 350),
    (15, 2, '2010-07-06', 300),
    (16, 2, '2010-07-07', 300),
    (17, 2, '2010-07-08', 200),
    (18, 2, '2010-07-09', 200);
    

    With that information stored, a simple SELECT query with a JOIN will show you the all the daily room rates:

    SELECT
        room.name,
        rate.date,
        rate.rate
    FROM room
    JOIN rate
    ON rate.room_id = room.id;
    
    +------+------------+--------+
    | A    | 2010-07-01 | 200.00 |
    | A    | 2010-07-02 | 200.00 |
    | A    | 2010-07-03 | 150.00 |
    | A    | 2010-07-04 | 150.00 |
    | A    | 2010-07-05 | 150.00 |
    | A    | 2010-07-06 | 200.00 |
    | A    | 2010-07-07 | 200.00 |
    | A    | 2010-07-08 | 100.00 |
    | A    | 2010-07-09 | 100.00 |
    | B    | 2010-07-01 | 300.00 |
    | B    | 2010-07-02 | 300.00 |
    | B    | 2010-07-03 | 250.00 |
    | B    | 2010-07-04 | 250.00 |
    | B    | 2010-07-05 | 350.00 |
    | B    | 2010-07-06 | 300.00 |
    | B    | 2010-07-07 | 300.00 |
    | B    | 2010-07-08 | 200.00 |
    | B    | 2010-07-09 | 200.00 |
    +------+------------+--------+
    

    To find the start and end dates for each room rate, you need a more complex query:

    SELECT 
        id,
        room_id,
        MIN(date) AS start_date,
        MAX(date) AS end_date,
        COUNT(*) AS days,
        rate
    FROM (
        SELECT
            id,
            room_id,
            date,
            rate, 
            (
                SELECT COUNT(*)
                FROM rate AS b
                WHERE b.rate <> a.rate
                AND b.date <= a.date
                AND b.room_id = a.room_id
            ) AS grouping
        FROM rate AS a
        ORDER BY a.room_id, a.date
    ) c
    GROUP BY rate, grouping
    ORDER BY room_id, MIN(date);
    
    +----+---------+------------+------------+------+--------+
    | id | room_id | start_date | end_date   | days | rate   |
    +----+---------+------------+------------+------+--------+
    |  1 |       1 | 2010-07-01 | 2010-07-02 |    2 | 200.00 |
    |  3 |       1 | 2010-07-03 | 2010-07-05 |    3 | 150.00 |
    |  6 |       1 | 2010-07-06 | 2010-07-07 |    2 | 200.00 |
    |  8 |       1 | 2010-07-08 | 2010-07-09 |    2 | 100.00 |
    | 10 |       2 | 2010-07-01 | 2010-07-02 |    2 | 300.00 |
    | 12 |       2 | 2010-07-03 | 2010-07-04 |    2 | 250.00 |
    | 14 |       2 | 2010-07-05 | 2010-07-05 |    1 | 350.00 |
    | 15 |       2 | 2010-07-06 | 2010-07-07 |    2 | 300.00 |
    | 17 |       2 | 2010-07-08 | 2010-07-09 |    2 | 200.00 |
    +----+---------+------------+------------+------+--------+
    

    You can find a good explanation of the technique used in the above query here:
    http://www.sqlteam.com/article/detecting-runs-or-streaks-in-your-data

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

Sidebar

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.