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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:37:36+00:00 2026-06-18T10:37:36+00:00

I am developing an Inventory system, using average cost method, where average cost price

  • 0

I am developing an Inventory system, using average cost method, where average cost price of product changes on each new purchase. Database I am using is SQL Server 2008

Now I need to calculate COST OF GOODS SOLD in a report, where I have to Add Cost Price for Each Sale of a product, but this COST PRICE should be relevant to a that specific Purchase period.

My Purchase Table

Purchase_Date   Product_ID   Ave_Cost_Price
1-jan-2013      1                  5.5
15-jan-2013     1                  6.5
30-jan-2013     1                  7.5

My Sales Table

Sale_Date   Product_ID    Sale_Price Cost_Price
5-jan-2013      1              10         ?         SALE-1
17-jan-2013     1              10         ?         SALE-1
31-jan-2013     1              15         ?         SALE-1

Now when I create a SALE report, SALE-1 should take 5.5, SALE-2, 6.5 and SALE-3 should pick 7.5 as Product cost price. and if it cannot find any purchase then it should pick opening Cost_Price from product_table.

I am looking for Such a Query which should do the job???

I think may be it can be done by some grouping and inner joins, but cannot figured it out.

any suggestions please ????

regards Raza

  • 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-18T10:37:37+00:00Added an answer on June 18, 2026 at 10:37 am

    I’m not a big fan of correlated subqueries in the select clause. Part of the reason is aesthetic and mmaintainability. I prefer to have all table references in one place, in the from clause. Part of the reason is performance; I tend to think that they turn into nested loop joins (usually for worse). Admittedly, SQL optimizers have gotten much better in the last few years.

    But this is one case where I think it is the best approach:

    [trouble uploading]

    The correlated subquery is . . .

    select s.*,
    

               (select top 1 ave_cost_price
    from purchase p
                where p.purchase_date <= sys.sale_date and p.product_id = s.product_id
                order by p.purchase_date desc
               ) a purchaseprice
    from sales s

    And the performance should be fine if you have an index on purchase(product_id, purchase_date) or even purchase(product_id, purchase_date, ave_cost_price).

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

Sidebar

Related Questions

For developing an devices monitor system, I am using a InetAdress isReachable method to
I am currently developing a database storage solution for product inventory information for the
We're developing a system to track inventory at our field offices. At its core,
We are developing an inventory tracking system. Basically we've got an order table in
Using ASP.NET MVC3 C# AND MS SQL 2008 I'm developing a Software As Service
I am building a site using code igniter. I am developing a custom product
developing a web application using java. I have SQL queries with more than 40
While developing an application using gwt in ecliplse crashed. Now the server is running
I am developing stock inventory for Bicycles. I stored Cycles cid,title, desc etc ,
I've got a question regarding the pros and cons of using a database vs.

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.