mysql - 计算日期之间的销售利润
mysql - Calculating profit of sales between dates
我有一个 table 看起来像这样:
+--------+---------------------+-------+--------+-----------+
| PartNo | Date | Inv | Retail | Wholesale |
+--------+---------------------+-------+--------+-----------+
| 1 | 2018-05-12 00:00:00 | 15 | 0 | |
| 2 | 2018-05-12 00:00:00 | 20 | 0 | 0 |
| 3 | 2018-05-12 00:00:00 | 25 | 0 | 0 |
| 1 | 2018-05-13 00:00:00 | 10 | | |
| 2 | 2018-05-14 00:00:00 | 15 | 0 | 0 |
| 3 | 2018-05-14 00:00:00 | 20 | 0 | 0 |
+--------+---------------------+-------+--------+-----------+
我希望通过 Mysql 查询看起来像这样:
+--------+------+--------+
| PartNo | Sold | Profit |
+--------+------+--------+
| 1 | 5 | |
| 2 | 5 | 0 |
| 3 | 5 | 0 |
+--------+------+--------+
我需要在计算一个日期范围内总计和利润之间的差异时按零件号分组。
单位利润必须通过从日期范围的最后一天(或记录)的零售中减去批发来计算。
我觉得这应该很容易,但日期范围内的差异让我感到困惑,并且处理日期范围内的记录不完全在输入的日期范围内开始或结束让我迷失了方向。
如有任何帮助,我们将不胜感激。
谢谢。
可以查看期初和期末情况,如果没有找到期初情况,则假设没有存货。如果没有发现结束情况,则表示该期间没有销售。
例如,对于从 2018-05-13
开始到 2018-05-14
结束的时间段:
select parts.PartNo
, coalesce(FirstSale.Total, 0) - coalesce(LastSale.Total, FirstSale.Total, 0) as Sold
, (coalesce(FirstSale.Total, 0) - coalesce(LastSale.Total, FirstSale.Total, 0)) *
coalesce(LastSale.Retail - LastSale.Wholesale, 0) as Profit
from (
select PartNo
, max(case when Date < '2018-05-13' then Date end) as FirstEntry
, max(case when Date <= '2018-05-14' then Date end) as LastEntry
from Sales
group by
PartNo
) parts
left join
Sales FirstSale
on FirstSale.PartNo = parts.PartNo
and FirstSale.Date = parts.FirstEntry
left join
Sales LastSale
on LastSale.PartNo = parts.PartNo
and LastSale.Date = parts.LastEntry
SELECT c.partno as partno,MAX(c.inv)-MIN(c.inv) as sold,SUM(CASE WHEN c.date = c.last_date THEN profit else 0 END)*(MAX(c.inv)-MIN(c.inv)) as profit
FROM (SELECT partno,date,inv,retail-wholesale as profit,MAX(date) OVER (partition by partno) AS last_date FROM test1)c
GROUP BY c.partno
ORDER BY c.partno;
使用 window 函数,首先附加一个新列来跟踪每个 partno 的最大日期。因此 FROM 中的内部查询将生成像这样的行,其中一列添加到原始数据集,
| 1 | 2018-05-12 00:00:00 | 15 | 0 | | **2018-05-13 00:00:00** |
突出显示的字段是添加到数据集的字段,它是该零件号日期范围内的最后一个日期!
现在从这个结果中,我们可以通过检查日期列等于我们附加的新列的行来提取利润,这实际上是通过从零售中减去批发并乘以计算最后日期的利润与出售的物品。
PS:销售商品的逻辑是按 partno 分组并从 MAX(Inv)
中减去 MIN(Inv)
我有一个 table 看起来像这样:
+--------+---------------------+-------+--------+-----------+
| PartNo | Date | Inv | Retail | Wholesale |
+--------+---------------------+-------+--------+-----------+
| 1 | 2018-05-12 00:00:00 | 15 | 0 | |
| 2 | 2018-05-12 00:00:00 | 20 | 0 | 0 |
| 3 | 2018-05-12 00:00:00 | 25 | 0 | 0 |
| 1 | 2018-05-13 00:00:00 | 10 | | |
| 2 | 2018-05-14 00:00:00 | 15 | 0 | 0 |
| 3 | 2018-05-14 00:00:00 | 20 | 0 | 0 |
+--------+---------------------+-------+--------+-----------+
我希望通过 Mysql 查询看起来像这样:
+--------+------+--------+
| PartNo | Sold | Profit |
+--------+------+--------+
| 1 | 5 | |
| 2 | 5 | 0 |
| 3 | 5 | 0 |
+--------+------+--------+
我需要在计算一个日期范围内总计和利润之间的差异时按零件号分组。
单位利润必须通过从日期范围的最后一天(或记录)的零售中减去批发来计算。
我觉得这应该很容易,但日期范围内的差异让我感到困惑,并且处理日期范围内的记录不完全在输入的日期范围内开始或结束让我迷失了方向。
如有任何帮助,我们将不胜感激。
谢谢。
可以查看期初和期末情况,如果没有找到期初情况,则假设没有存货。如果没有发现结束情况,则表示该期间没有销售。
例如,对于从 2018-05-13
开始到 2018-05-14
结束的时间段:
select parts.PartNo
, coalesce(FirstSale.Total, 0) - coalesce(LastSale.Total, FirstSale.Total, 0) as Sold
, (coalesce(FirstSale.Total, 0) - coalesce(LastSale.Total, FirstSale.Total, 0)) *
coalesce(LastSale.Retail - LastSale.Wholesale, 0) as Profit
from (
select PartNo
, max(case when Date < '2018-05-13' then Date end) as FirstEntry
, max(case when Date <= '2018-05-14' then Date end) as LastEntry
from Sales
group by
PartNo
) parts
left join
Sales FirstSale
on FirstSale.PartNo = parts.PartNo
and FirstSale.Date = parts.FirstEntry
left join
Sales LastSale
on LastSale.PartNo = parts.PartNo
and LastSale.Date = parts.LastEntry
SELECT c.partno as partno,MAX(c.inv)-MIN(c.inv) as sold,SUM(CASE WHEN c.date = c.last_date THEN profit else 0 END)*(MAX(c.inv)-MIN(c.inv)) as profit
FROM (SELECT partno,date,inv,retail-wholesale as profit,MAX(date) OVER (partition by partno) AS last_date FROM test1)c
GROUP BY c.partno
ORDER BY c.partno;
使用 window 函数,首先附加一个新列来跟踪每个 partno 的最大日期。因此 FROM 中的内部查询将生成像这样的行,其中一列添加到原始数据集,
| 1 | 2018-05-12 00:00:00 | 15 | 0 | | **2018-05-13 00:00:00** |
突出显示的字段是添加到数据集的字段,它是该零件号日期范围内的最后一个日期!
现在从这个结果中,我们可以通过检查日期列等于我们附加的新列的行来提取利润,这实际上是通过从零售中减去批发并乘以计算最后日期的利润与出售的物品。
PS:销售商品的逻辑是按 partno 分组并从 MAX(Inv)
中减去 MIN(Inv)