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

  • Home
  • SEARCH
  • 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 109865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:08:12+00:00 2026-05-11T02:08:12+00:00

I’m trying to generate a sales reports which lists each product + total sales

  • 0

I’m trying to generate a sales reports which lists each product + total sales in a given month. Its a little tricky because the prices of products can change throughout the month. For example:

  • Between Jan-01 and Jan-15, my company sells 50 Widgets at a cost of $10 each
  • Between Jan-15 and Jan-31, my company sells 50 more Widgets at a cost of $15 each
  • The total sales of Widgets for January = (50 * 10) + (50 * 15) = $1250

This setup is represented in the database as follows:

 Sales table   Sale_ID    ProductID    Sale_Date   1          1            2009-01-01   2          1            2009-01-01   3          1            2009-01-02              ...   50         1            2009-01-15   51         1            2009-01-16   52         1            2009-01-17              ...   100        1            2009-01-31  Prices table   Product_ID    Sale_Date    Price   1             2009-01-01   10.00   1             2009-01-16   15.00 

When a price is defined in the prices table, it is applied to all products sold with the given ProductID from the given SaleDate going forward.

Basically, I’m looking for a query which returns data as follows:

 Desired output   Sale_ID    ProductID    Sale_Date     Price   1          1            2009-01-01    10.00   2          1            2009-01-01    10.00   3          1            2009-01-02    10.00              ...   50         1            2009-01-15    10.00   51         1            2009-01-16    15.00   52         1            2009-01-17    15.00              ...   100        1            2009-01-31    15.00 

I have the following query:

SELECT     Sale_ID,     Product_ID,     Sale_Date,     (         SELECT TOP 1 Price         FROM Prices         WHERE             Prices.Product_ID = Sales.Product_ID             AND Prices.Sale_Date < Sales.Sale_Date          ORDER BY Prices.Sale_Date DESC     ) as Price FROM Sales 

This works, but is there a more efficient query than a nested sub-select?

And before you point out that it would just be easier to include ‘price’ in the Sales table, I should mention that the schema is maintained by another vendor and I’m unable to change it. And in case it matters, I’m using SQL Server 2000.

  • 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. 2026-05-11T02:08:13+00:00Added an answer on May 11, 2026 at 2:08 am

    If you start storing start and end dates, or create a view that includes the start and end dates (you can even create an indexed view) then you can heavily simplify your query. (provided you are certain there are no range overlaps)

    SELECT     Sale_ID,     Product_ID,     Sale_Date,     Price FROM Sales JOIN Prices on Sale_date > StartDate and Sale_Date <= EndDate   -- careful not to use between it includes both ends  

    Note:

    A technique along these lines will allow you to do this with a view. Note, if you need to index the view, it will have to be juggled around quite a bit ..

    create table t (d datetime)  insert t values(getdate()) insert t values(getdate()+1) insert t values(getdate()+2)  go create view myview  as select start = isnull(max(t2.d), '1975-1-1'), finish = t1.d  from t t1 left join t t2 on t1.d > t2.d group by t1.d  select * from myview   start                   finish ----------------------- ----------------------- 1975-01-01 00:00:00.000 2009-01-27 11:12:57.383 2009-01-27 11:12:57.383 2009-01-28 11:12:57.383 2009-01-28 11:12:57.383 2009-01-29 11:12:57.383 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary

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.