N Mosin
asked on
SQL queries for sales / expenses / profit calculation
I have DAILY sales data for 3 products in this form: TABLE1
Product1|SaleAmount|UnitsS old|TimeSt amp (there are multiple records for each day)
Product2|SaleAmount|UnitsS old|TimeSt amp (there are multiple records for each day)
Product2|SaleAmount|UnitsS old|TimeSt amp (there are multiple records for each day)
I have DAILY expense data for 3 products in this form: TABLE2
Product1|ExpenseAmount|Exp enseType|T imeStamp (there are multiple records for each day)
Product2|ExpenseAmount|Exp enseType|T imeStamp (there are multiple records for each day)
Product2|ExpenseAmount|Exp enseType|T imeStamp (there are multiple records for each day)
TimeStamp in in YEAR:MONTH:DAY:HH:MM:SS
I need a few SQL statements:
1. I want to show the following columns on a MONTH BY MONTH basis for each product:
Product1|SaleAmountForMont h|UnitsSol dForMonth
Product2|SaleAmountForMont h|UnitsSol dForMonth
Product3|SaleAmountForMont h|UnitsSol dForMonth
Thus, the rows will contain the name of each product; there will be ONE row for each product for each month.
The Month will show as Jan-2018, Feb-2018
2. I also want to show the following columns on a MONTH BY MONTH basis for each product:
Product1|ExpenseAmountForM onth|Expen seTypeForM onth
Product2|ExpenseAmountForM onth|Expen seTypeForM onth
Product3|ExpenseAmountForM onth|Expen seTypeForM onth
Thus, the rows will contain the name of each product; there will be MULTIPLE ROWS for each product for each month, i.e. the number of rows for each month will equal the number of expenses incurred. The Month will show as Jan-2018, Feb-2018
3. For each month, I then want to show the profit for each product in a table. The formula for each product:
Total sales for that product for that month - Total expenses of ALL types for that product for that month
Will I need to use a UNION or JOIN for this? Please note that I do not need to persist this information anywhere.
Product1|SaleAmount|UnitsS
Product2|SaleAmount|UnitsS
Product2|SaleAmount|UnitsS
I have DAILY expense data for 3 products in this form: TABLE2
Product1|ExpenseAmount|Exp
Product2|ExpenseAmount|Exp
Product2|ExpenseAmount|Exp
TimeStamp in in YEAR:MONTH:DAY:HH:MM:SS
I need a few SQL statements:
1. I want to show the following columns on a MONTH BY MONTH basis for each product:
Product1|SaleAmountForMont
Product2|SaleAmountForMont
Product3|SaleAmountForMont
Thus, the rows will contain the name of each product; there will be ONE row for each product for each month.
The Month will show as Jan-2018, Feb-2018
2. I also want to show the following columns on a MONTH BY MONTH basis for each product:
Product1|ExpenseAmountForM
Product2|ExpenseAmountForM
Product3|ExpenseAmountForM
Thus, the rows will contain the name of each product; there will be MULTIPLE ROWS for each product for each month, i.e. the number of rows for each month will equal the number of expenses incurred. The Month will show as Jan-2018, Feb-2018
3. For each month, I then want to show the profit for each product in a table. The formula for each product:
Total sales for that product for that month - Total expenses of ALL types for that product for that month
Will I need to use a UNION or JOIN for this? Please note that I do not need to persist this information anywhere.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Open in new window