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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:54:21+00:00 2026-06-13T14:54:21+00:00

Long time reader, first time poster. Hoping some SQL guru can lend me some

  • 0

Long time reader, first time poster. Hoping some SQL guru can lend me some processing power to solve a SQL problem elegantly and without cursors.

I am trying to create a time line type result set from a data set with money and a Date, where the order in which they appear is important. See sample data and expectation below.

DECLARE @OrderData TABLE(
      ID                      INT IDENTITY,
      ProductId               INT,
      WarehouseId             INT,
      Cost MONEY,
      SaleDate          DATETIME
)

 

INSERT INTO @OrderData
VALUES
      (1, 1, 2.71, '2012-02-23 10:01')
      ,(1, 2, 2.71, '2012-02-23 10:02')
      ,(1, 1, 2.71, '2012-02-23 10:03')
      ,(1, 1, 2.71, '2012-02-23 10:04')
      ,(1, 1, 2.71, '2012-02-23 10:05')
      ,(1, 1, 2.8, '2012-02-23 10:06')
      ,(1, 1, 2.9, '2012-02-23 10:07')
      ,(1, 1, 2.71, '2012-02-23 10:08')
      ,(1, 1, 2.71, '2012-02-23 10:09')

The result I am looking for is a time line of sales with from and to dates for a product and warehouse combination within it own set of sales. As soon as the sale price changes in the dataset this should spawn a new row in the result set.

Result structure for sample data.

Product, warehouse, minsale date, Max sale date
1,1,2.71, 2012-02-23 10.01, 2012-02-23 10.05
1,1,2.80, 2012-02-23 10.06, 2012-02-23 10.06
1,1,2.90, 2012-02-23 10.07, 2012-02-23 10.07
1,1,2.71, 2012-02-23 10.08, 2012-02-23 10.09
1,2,2.71, 2012-02-23 10.02, 2012-02-23 10.02

Cheers for any help available.

  • 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-13T14:54:23+00:00Added an answer on June 13, 2026 at 2:54 pm

    This is a “gaps and islands” problem.

    select ProductID, WarehouseId, cost, MIN(saledate), MAX(saledate) from
    (      
        select *,
            ROW_NUMBER() over (order by saledate)-
            ROW_NUMBER() over (partition by cost order by saledate) grp
        from @OrderData
    ) v
    group by ProductID, WarehouseId, grp, cost
    order by WarehouseID, MIN(SaleDate)
    

    For further information, search for “gaps and islands by itzik ben-gan”

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

Sidebar

Related Questions

First time poster, long time reader. I've been having a problem with figuring this
Long time reader, first time poster. Any help is greatly appreciated. I have crafted
first time poster, long time reader so be gentle with me :) See the
long time reader, first time poster. I’m coming with an issue that many of
Long time reader, first time poster. Working on a web site at http://www.howardpitch.com/ ,
Long time reader, first time poster. I'm a big noob when it comes to
Long time reader, first time poster asks: After many weeks of google and forum
I'm a long time reader first time poster, glad to start participating in this
Long time reader, first time poster. One day I hope to be answering questions
First time poster, long time reader. I searched and found no questions that answered

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.