从我的数据库中获取营业额,收入,总费用并将其按年份分组

时间:2018-11-05 08:25:37

标签: mysql sql

我有4张桌子:

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

产品有4列:idProd是主键

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

支出-3列,idSpd是主键

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

销售-5列,其中idSale作为主键,idProd将其链接到产品表

+--------+--------+--------------+----------+------------+
| 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 |
+--------+--------+--------------+----------+------------+

和directv:

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

我想举个例子:

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

我的查询:

--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) 

和收入=产品收益+ Directv-支出

我想加入所有这些查询并计算income。我昨天用@Gordon Linoff here尝试了没有餐桌产品,这没关系,但是今天我无法与我的新餐桌产品结合使用。

1 个答案:

答案 0 :(得分:0)

将所有已经运行的查询(并且具有相同的月/年主题)合并在一起:

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

这是另一种写同一件事的方法:

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

使用喜欢的外观;)

现在,这些查询只是SELECT *来自所有相关子查询的数据-您还有很多工作要做:a)删除moyr的多次出现,以及b)在任何列上进行数学运算,例如select gain.gain + directv.directv - spend.spend as income..,但是一旦您可以看到同一行中的所有数据,那应该足够简单

重要:如果这些查询中的任何一个在一个月内都不会返回任何结果,请不要使用内部联接,因为它将导致所有月份的统计信息消失。选择绝对不会缺少月份的查询,然后将其他子查询移至该查询中

如果永远不会有这样的月份,即这些查询中的任何一个有一天有一天都不会返回一个月的结果,则有可能在算法上建立另一个月份列表,并将所有子查询加入其中。这是一个有关如何执行此操作的问题:

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