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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:27:52+00:00 2026-06-01T11:27:52+00:00

I have a table with 3 colums. Here is an example with just a

  • 0

I have a table with 3 colums. Here is an example with just a few entries to demo. The table represents the dates that an item’s price changes. I need a query that will tell me the prices for all the items on a specific date. The specific date may be between price changes. The price changes at midnight, so the date of the change, that is the price from then until and on the day before the previous change:

itemCode  datePriceEffective  price  
AB         2012-01-01          9.99  
AB         2012-03-02          10.50  
XY         2011-09-20          34.99  

I wish to get all the items prices for a given date. There is not an entry for every date, just the dates that the price changes.
so 2012-03-05 would return

AB 10.50
XY 34.99

and 2012-02-27 would return:

AB 9.99  
XY 34.99

The SQL query needed to get this escapes me. Answers appreciated.

Edited as answers are going in wrong direction see italics for edit.

  • 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-01T11:27:53+00:00Added an answer on June 1, 2026 at 11:27 am

    You will need to join back to the table as follows:

    declare @effdate datetime
    set @effdate = CONVERT(datetime,'2012-02-27')
    
    ;WITH CTE_DATA as (
        select itemCode='AB',  datePriceEffective = CONVERT(Datetime,'2012-01-01'),  price = 9.99
        union all select itemCode='AB',  datePriceEffective = CONVERT(Datetime,'2012-03-02'),  price = 10.50
        union all select itemCode='XY',  datePriceEffective = CONVERT(Datetime,'2011-09-20'),  price = 34.99
    )
    select
        d.itemcode,
        price
    from
        CTE_DATA d
        join (
            select  
                itemcode,
                effdate = MAX(datepriceeffective)
            from CTE_DATA sub where sub.datepriceeffective <= @effdate
            group by itemcode
        ) x
            on x.itemCode = d.itemCode
            and x.effdate = d.datePriceEffective
    

    Note that the CTE is just for this example, you should swap it for your real table.

    UPDATE: An alternative approach is to use ROW_NUMBER and PARTITION as follows:

    SELECT        
        itemcode,
        price
    FROM     
       (
            SELECT   
                itemcode,
                price,
                rowno = ROW_NUMBER() over (partition by itemcode order by datePriceEffective desc)
            from
                CTE_DATA
                where datePriceEffective <= @effdate
        ) x
    where 
        rowno = 1
    

    (Substitute this select for the one in the previous query to try it out on the data)

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

Sidebar

Related Questions

Here is a simple example: Assume I have a customer table that has a
Here's the caveat... If you have a table that has a single column and
database/SQL novice here. I have a table with a column called, for example, EmployeeID.
Okay, here's the situation: We have a table of about 50 columns (created by
So here is the situation. In my db table i have a column called
i have day, month, year, value columns in one table, here i need to
i have table structure with 3 colums (column1, column2, column3) and i want to
MY base table have two colums assume col1,col2. col1 have id, col2 have many
i have a table Students which contains the following colums: Id,FirstName,LastName,Adress . The colum
I am having some weird issue here. I have a database table which has

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.