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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:22:57+00:00 2026-06-12T05:22:57+00:00

I have some data in a table that looks similar to this: Item Date

  • 0

I have some data in a table that looks similar to this:

Item    Date         Price
A       8/29/2012    $3
B       8/29/2012    $23
C       8/29/2012    $10
A       8/30/2012    $4
B       8/30/2012    $25
C       8/30/2012    $11
A       8/31/2012    $3
B       8/31/2012    $22
C       8/31/2012    $8
A       9/01/2012    $3
B       9/01/2012    $26
C       9/01/2012    $9
A       9/02/2012    $3
B       9/02/2012    $24
C       9/02/2012    $9

I need to write a query that identifies that price for A hasn’t changed
since 8/30/2012, price for item C hasn’t changed since 9/01/2012, and
to return the number of days elapsed for both (we’re looking for items
with non-moving prices). I can’t use CTE, or cursors, or separately created
temp tables (select into, etc) because of the limitations of the web report
tool this sql needs to run in. I can only use a ‘basic’ single pass select query
(subqueries will work though). Does anyone have any cunning ideas on how to
achieve this??

My first attempt was to group by Item and Price, where price is the same as
the latest price, having count > 2, identifying the min date and doing a datediff
between min date and getdate. However this just identifies the first instance of
that price, it doesn’t take into account any subsequent rows that might have a
different price. Hope that makes sense.

  • 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-12T05:22:58+00:00Added an answer on June 12, 2026 at 5:22 am

    I’ve given both types of ages and account for items that exist only once in the table (they don’t have an old date). Please let us know how close this is.

    UPDATE: I had to correct the date calculation in “PriceAgeToNow”, and I’ve also tried to filter out records that are have only had a new price for 1 day. Here is the SQL Fiddle.

    -- Get the age of the current price
    select *
        , datediff(d, c.OldDate, getdate()) as PriceAgeToNow
        , datediff(d, c.OldDate, c.NewDate) as PriceAgeToNewestDate
    from (
        select *
            -- Get max date of former price
            , isnull(
                (select max(Date) from PricingTable where Item = b.Item and Date < b.NewDate and Price != b.NewPrice), b.NewDate
            ) as OldDate
        from (
            -- Get current price
            select *
                , (select Price from PricingTable where Item = a.Item and Date = a.NewDate) as NewPrice
            from (
                -- Get current date
                select Item
                    , max(Date) as NewDate
                from PricingTable
                group by Item
            ) a
        ) b
    ) c
    -- Attempt to filter out price changes that have only lasted 1 day
    where datediff(d, c.OldDate, c.NewDate) > 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data in a table that looks roughly like the following: table
All, I have some PHP PDO code that looks like this: <?php ... $query=$dbh->prepare(SELECT
I have some data that looks like this: 1: Events 2: Event 1 3:
I have some data that I have to display as a table. I think
I have an Excel table with some data that I would like to export
I have a table from a vendor application that stores some xml data into
I have a table in html with some tabular data. The thing is that
I have a pivot table with some data that im trying to reference with
I have some PHP code that generates dynamic tables of data on the fly.
I have a data-upload function that load some data into several tables and processes

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.