简体   繁体   中英

Get the turnover, income, total expenses from my database and group them by year-month

I have 4 tables :

Products - to record products
spending - to record expenses
directv  - to record subscription payments to TV channels
sales    - to record sales

Products has 4 columns : idProd is the primary key

+--------+-----------+--------------+-------------+
| idProd | nameProd  | sellingPrice | buyingPrice |
+--------+-----------+--------------+-------------+
|      1 | Iphone 8  |          500 |         400 |
|      2 | Samsung 2 |          600 |         400 |
+--------+-----------+--------------+-------------+

Spending - 3 columns and idSpd is the primary key

+-------+--------+------------+
| idSpd | amount | dateSpd    |
+-------+--------+------------+
|     1 |   1000 | 2018-11-01 |
|     2 |   1000 | 2018-11-01 |
|     3 |   1000 | 2018-10-01 |
|     4 |   4000 | 2018-10-01 |
+-------+--------+------------+

Sales - 5 columns with idSale as primary key and idProd to link it to the product's table

+--------+--------+--------------+----------+------------+
| idSale | idProd | sellingPrice | quantity | dateSale   |
+--------+--------+--------------+----------+------------+
|      1 |      1 |          700 |        2 | 2018-11-01 |
|      2 |      1 |          700 |        5 | 2018-11-15 |
|      3 |      2 |          800 |        2 | 2018-11-16 |
+--------+--------+--------------+----------+------------+

and directv :

+-------+-----------------+-------+------------+
| idDtv | brand           | price | dateDtv    |
+-------+-----------------+-------+------------+
|     1 | channel decoder |   150 | 2018-11-09 |
+-------+-----------------+-------+------------+

and I wish to get for example :

income|gain of product  |  turnover | Spending | DirecTv | month | year
1000  |     400         | 5500      | 3000     | 50      | 10     |2018
500   |     200         |  1000     | 2000     | 0       | 11     |2018

my queries :

--for directv
select sum(dv.price) , month(dv.dateDtv), year(dv.dateDtv) from directv dv GROUP by year(dv.dateDtv) , month(dv.dateDtv) 

--for turnover
select sum(sl.quantity*sl.sellingPrice) , month(sl.dateSale) , year(sl.dateSale) from sales sl GROUP by year(sl.dateSale) , month(sl.dateSale)

--for spending
select sum(spd.amount) , month(spd.dateSpd) , year(spd.dateSpd) from spending spd GROUP by year(spd.dateSpd) , month(spd.dateSpd) 

--for gain of product
SELECT sum(s.quantity*(s.sellingPrice-p.buyingPrice)) from sales s JOIN products p on s.idProd = p.idProd GROUP by year(s.dateSale) , month(s.dateSale) 

and income = gain of product + directv - spending

I want to join all these queries and calculate the income . I tried without the table product yesterday with @Gordon Linoff here it was okay, but I was unable to combine with my new table product today.

Make all your queries that already work (and have a common theme of the month/year) joined together:

SELECT * 
FROM
  (select sum(dv.price) directv, month(dv.dateDtv) as mo, year(dv.dateDtv) as yr from directv dv GROUP by year(dv.dateDtv) , month(dv.dateDtv)) as directv
  INNER JOIN
  (select sum(sl.quantity*sl.sellingPrice) as turnover, month(sl.dateSale) as mo, year(sl.dateSale) as yr from sales sl GROUP by year(sl.dateSale) , month(sl.dateSale)) as turnover
  ON directv.mo = turnover.mo and directv.yr = turnover.yr

  INNER JOIN
  (select sum(spd.amount) as spending, month(spd.dateSpd) as mo, year(spd.dateSpd) as yr from spending spd GROUP by year(spd.dateSpd) , month(spd.dateSpd)) as spending
  ON directv.mo = spending.mo and directv.yr = spending.yr

  INNER JOIN
  (SELECT sum(s.quantity*(s.sellingPrice-p.buyingPrice)) as gain, year(s.dateSale) as yr, month(s.dateSale) as mo from sales s JOIN products p on s.idProd = p.idProd GROUP by year(s.dateSale) , month(s.dateSale) ) as gain
  ON directv.mo = gain.mo and directv.yr = gain.yr

Here's another way of writing the same thing:

WITH
  directv as (select sum(dv.price) directv, month(dv.dateDtv) as mo, year(dv.dateDtv) as yr from directv dv GROUP by year(dv.dateDtv) , month(dv.dateDtv)),
  turnover as (select sum(sl.quantity*sl.sellingPrice) as turnover, month(sl.dateSale) as mo, year(sl.dateSale) as yr from sales sl GROUP by year(sl.dateSale), month(sl.dateSale)),
  spending as (select sum(spd.amount) as spending, month(spd.dateSpd) as mo, year(spd.dateSpd) as yr from spending spd GROUP by year(spd.dateSpd) , month(spd.dateSpd)),
  gain as (SELECT sum(s.quantity*(s.sellingPrice-p.buyingPrice)) as gain, year(s.dateSale) as yr, month(s.dateSale) as mo from sales s JOIN products p on s.idProd = p.idProd GROUP by year(s.dateSale) , month(s.dateSale) )

SELECT * FROM
  directv
  INNER JOIN turnover ON directv.mo = turnover.mo and directv.yr = turnover.yr
  INNER JOIN spending ON directv.mo = spending.mo and directv.yr = spending.yr
  INNER JOIN ON directv.mo = gain.mo and directv.yr = gain.yr

Use whichever one you prefer the look of ;)

Now, these queries just SELECT * the data from all the relevant subqueries - there's still a bit of work for you to do to a) remove the multiple occurrences of mo and yr and b) do the maths on any columns, eg select gain.gain + directv.directv - spend.spend as income.. but that should be simple enough once you can see the data all on the same row

Important : If any of these queries will ever return no results for a month, don't use inner join as it will cause all the month stats to disappear. Pick the query that absolutely will never ever have a missing month, and LEFT JOIN the other subqueries to it instead

If there will never be such a month, ie there is a chance that ANY of these queries will one day return no results for a month, then establish another list of months algorithmically and left join all the subqueries onto it. Here is one such question that deals with how to do this:

How to get a list of months between two dates in mysql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM