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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:27:05+00:00 2026-06-05T13:27:05+00:00

I have two tables, namely Price List (Table A) and Order Record (Table B)

  • 0

I have two tables, namely Price List (Table A) and Order Record (Table B)

Table A

SKU Offer Date  Amt
AAA 20120115    22
AAA 20120223    24
AAA 20120331    25
AAA 20120520    28

Table B

Customer   SKU  Order Date
A001       AAA  20120201
B001       AAA  20120410
C001       AAA  20120531

I have to retrieve the correct pricing for each customer based on the order date. The expected output should be like this:-

Customer  SKU   Order Date  Amt
A001      AAA   20120201    22
B001      AAA   20120410    25
C001      AAA   20120531    28

Thanks.

  • 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-05T13:27:07+00:00Added an answer on June 5, 2026 at 1:27 pm

    A left join (or NOT EXISTS subquery) can be used to ensure that the join between the two tables uses the “most recent” row from the prices table that is dated on or before the order date. I assume that’s the relationship between the tables that you want to achieve:

    Setup:

    create table Prices (
        SKU char(3) not null,
        OfferDate date not null,
        Amt int not null
    )
    go
    insert into Prices (SKU, OfferDate,  Amt) values
    ('AAA','20120115',    22),
    ('AAA','20120223',    24),
    ('AAA','20120331',    25),
    ('AAA','20120520',    28)
    go
    create table Orders (
        Customer char(4) not null,
        SKU char(3) not null,
        OrderDate date not null
    )
    go
    insert into Orders (Customer,   SKU,  OrderDate) values
    ('A001','AAA','20120201'),
    ('B001','AAA','20120410'),
    ('C001','AAA','20120531')
    go
    

    Query:

    select
        o.*, /* TODO - Explicit columns */
        p.Amt
    from
        Orders o
            inner join
        Prices p
            on
                o.SKU = p.SKU and
                o.OrderDate >= p.OfferDate
            left join
        Prices p_later
            on
                o.SKU = p_later.SKU and
                o.OrderDate >= p_later.OfferDate and
                p_later.OfferDate > p.OfferDate 
    where
        p_later.SKU is null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My prob in brief: I have two tables namely category and product. table: category
I have two tables namely States, Package_Details. Below are the details of the table.
I have two tables, for example: Table A Table B ======= ======= Name |
I have two tables: a schedule table that contains information about how an employee
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay
I have two tables CREATE TABLE table1 (id int primary key auto_increment,....); CREATE TABLE
I have two tables namely 'categories' & 'products' as follows: categories id | name
I have two columns in table users namely registerDate and lastVisitDate which consist of
Have two tables named Employee and Order. The relations between the tables Employee (1)

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.