Skip to content
Advertisement

SQL Query Sum, Multiply and combine 5 tables then group

I’ve created my table. I’m trying to craate a query that multiply and add sold_quantity from sold table and sale_price from on_sale table and called it R1 for now, and retail_price from product table and sold_quantity from sold table called it R2 for now.
In my query, I want to calculate my revenue. The catch is there’s two different date but one sale quantity. That means it’s kinda hard for me to distinguish twO types of sales( discounted sale, retail sale).
For example, on Feb.1st, I have a sale going on, I sold 10 quantity, and price sold is as sale_price and date is saved as sale_date, refer to On_sale table. On Feb.2, I sold 8 quantity , but price sold is saved as retail_price and saved as sold_date.


This is what I want SAMPLE DATA

Explanation of my data
: This is very confusing. First I need to display year based on sold date and sale date, then calculate revenue. For example,in year 2018, the revneue is (2 from on_sale table’s sale_price) + (12 (3 * 4, 3 is the sold_quantity from sold_table, and 4 is retail_price) = 14.
The city size is separated by ranges, where 0>#<100 is small 100>=x<200 is medium and anything above 200 is large. the city name in the parenthesis is just to help track. The city is based on the city name and state in store table, and that is doen by comparing store_number on both sold table and store table
This requires me to join city table after querying to get R1(normal price) and R2(on sale price). Here’s what I got. I’m very lost

Advertisement

Answer

I don’t understand the use of LEFT JOIN in your queries. You seem to have a well-defined data model and the ids should line up.

You specifically say that you want to add R1 and R2, so the query you describe looks like this:

Note that this adds an extra condition saying that the dates match for the on_sale table. That makes sense to me.

Adding these two revenue numbers seems very suspicious to me. I would expect that you would want the sales price, if it exists, and otherwise the retail price. If so then the calculation would be:

If you want the sum of the discount amounts, then the difference makes sense.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement