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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:54:38+00:00 2026-06-02T22:54:38+00:00

With the following data create table #ph (product int, [date] date, price int) insert

  • 0

With the following data

create table #ph (product int, [date] date, price int)
insert into #ph select 1, '20120101', 1
insert into #ph select 1, '20120102', 1
insert into #ph select 1, '20120103', 1
insert into #ph select 1, '20120104', 1
insert into #ph select 1, '20120105', 2
insert into #ph select 1, '20120106', 2
insert into #ph select 1, '20120107', 2
insert into #ph select 1, '20120108', 2
insert into #ph select 1, '20120109', 1
insert into #ph select 1, '20120110', 1
insert into #ph select 1, '20120111', 1
insert into #ph select 1, '20120112', 1

I would like to produce the following output:

product | date_from | date_to  | price
  1     | 20120101  | 20120105 |   1
  1     | 20120105  | 20120109 |   2
  1     | 20120109  | 20120112 |   1

If I group by price and show the max and min date then I will get the following which is not what I want (see the over lapping of dates).

product | date_from | date_to  | price
  1     | 20120101  | 20120112 |   1
  1     | 20120105  | 20120108 |   2

So essentially what I’m looking to do is group by the step change in data based on group columns product and price.

What is the cleanest way to achieve this?

  • 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-02T22:54:49+00:00Added an answer on June 2, 2026 at 10:54 pm

    There’s a (more or less) known technique of solving this kind of problem, involving two ROW_NUMBER() calls, like this:

    WITH marked AS (
      SELECT
        *,
        grp = ROW_NUMBER() OVER (PARTITION BY product        ORDER BY date)
            - ROW_NUMBER() OVER (PARTITION BY product, price ORDER BY date)
      FROM #ph
    )
    SELECT
      product,
      date_from = MIN(date),
      date_to   = MAX(date),
      price
    FROM marked
    GROUP BY
      product,
      price,
      grp
    ORDER BY
      product,
      MIN(date)
    

    Output:

    product  date_from   date_to        price 
    -------  ----------  -------------  ----- 
    1        2012-01-01  2012-01-04     1     
    1        2012-01-05  2012-01-08     2     
    1        2012-01-09  2012-01-12     1     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on the following data structure. CREATE TABLE [Parent] ( Id INT, Name nvarchar(100)
To create a table, I am retrieving the data with the following query: SELECT
I have the following data structure and data: CREATE TABLE `parent` ( `id` int(11)
I have the following table and data in SQL Server 2005: create table LogEntries
I have the following table: CREATE TABLE [dbo].[Data] ( [Id] UNIQUEIDENTIFIER NOT NULL, [Data]
I have two tables with the following schema: CREATE TABLE sales_data ( sales_time date
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
Given the following data table definitions in SQLite Version 3.7.5: CREATE TABLE [Books] (
I'm trying to insert data into a table using a plpgsql function or stored
I am trying to create a mapping for the following JSON data. { Calls:

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.