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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:45:55+00:00 2026-06-01T06:45:55+00:00

I have two tables with the following (simplified) structures: table "Factors" which holds data

  • 0

I have two tables with the following (simplified) structures:

  1. table "Factors" which holds data about purchased goods’ factors and has these columns:

    FactorSerial, PurchaseDate, PurchasedGood

  2. table "Prices" which holds goods prices on different dates

    Serial, GoodCode, EvaluationDate, Price

A price is valid until a new row with the same Code but different date is added and thus updates its value

Now, I want to create a table which adds the price to the table 1 according to purchase date.
So if we have:

  PurchaseDate  PurchasedGood
 -----------------------------
  05/20/2011      A111

and:

  GoodCode  EvaluationDate  Price
 --------------------------------
   A111      02/01/2011     100
...
   A111      04/01/2011     110
...
   A111      06/01/2011     120

the result would be

  PurchaseDate  PurchasedGood  Price
 -----------------------------------
  05/20/2011      A111          110

Preferred method is creating the view Prices1 as

 Serial  GoodCode   StartDate  EndDate  Price

and then joining Factors with this view by

  PurchasedDate between StartDate AND EndDate

Can anybody show me how to create view1 (or obtaining the final result with any other method)? Thanks in advance!

P.S. sorry for my bad English!

  • 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-01T06:45:56+00:00Added an answer on June 1, 2026 at 6:45 am

    I want to create a table which adds the price to the table 1 according to purchase date.

    Here is a query that returns such data. The syntax is pretty standard SQL, I believe, but this was tested on SQL Server (looks like you may be using PostgreSQL with your "serial" naming).

    select a.FactorSerial, a.PurchasedGood, a.PurchaseDate
        , (select max(Price) from Prices where GoodCode = a.PurchasedGood and EvaluationDate = a.EvaluationDate) as Price
    from (
        select f.FactorSerial, f.PurchasedGood, f.PurchaseDate, max(p.EvaluationDate) as EvaluationDate
        from Factors as f
            join Prices as p on f.PurchasedGood = p.GoodCode
        where f.PurchaseDate >= p.EvaluationDate
        group by f.FactorSerial, f.PurchasedGood, f.PurchaseDate
    ) as a
    

    This query assumes that there are no Purchases before a Price existed.

    Also, considering:

    Preferred method is creating the view Prices1 as

    Serial  GoodCode   StartDate  EndDate  Price
    

    and then joining Factors with this view by

     PurchasedDate between StartDate AND EndDate
    

    between is inclusive. Using this method that you’ve described, you would get duplicates if a PurchaseDate lies on the EndDate of one row and the StartDate of another.

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

Sidebar

Related Questions

I have the following two tables (simplified for this question): CREATE TABLE team (
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I have two tables invoices and pending_payments both of which have the following rows
I have two tables A,B which are related to each other (simplified): A: +-------+---------+
I have two tables as following table one named tbooking having column fields as
I have two tables that look like following: RELEASE TABLE name asin MATCHES TABLE
I have two tables. One of them called conversations has the following data. ConversationID
I have following two tables Table name: thread pk-> thread_id | thread_title | description
I have following two tables and a script having a function includePage() . Table
I have the following two (simplified for the sake of example) tables in my

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.