gpt4 book ai didi

mysql - 从我的数据库中获取营业额、收入、总支出,并按年月进行分组

转载 作者:行者123 更新时间:2023-11-29 16:44:38 26 4
gpt4 key购买 nike

我有 4 张 table :

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

和直接:

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

收入 = 产品 yield + 直接支出 - 支出

我想加入所有这些查询并计算收入。昨天我和@Gordon Linoff here一起尝试了不使用桌面产品的情况。没关系,但我今天无法与我的新 table 产品结合使用。

最佳答案

将所有已经有效的查询(并且具有月/年的共同主题)连接在一起:

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) 对任意列进行数学计算,例如选择gain.gain + directv.directv - spend.spend作为收入..但是一旦您可以看到同一行上的所有数据,这应该很简单

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

如果永远不会有这样的一个月,即这些查询中的任何一个都有可能有一天在一个月内没有返回结果,则通过算法建立另一个月份列表,并将所有子查询左连接到该列表上。这是一个涉及如何执行此操作的此类问题:

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

关于mysql - 从我的数据库中获取营业额、收入、总支出,并按年月进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53150712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com