Mysql 显示收入增加或减少百分比的Sql查询

Mysql 显示收入增加或减少百分比的Sql查询,mysql,sql,Mysql,Sql,我想编写一个sql查询,以百分比显示月份的收入 例如,我需要计算2016年11月添加的订单的price字段条目的sum(我得到了一个日期字段,时间戳类型) 然后计算从2016年12月到今天(一月,二月,三月等)的价格字段条目的总和) 然后以百分比的形式显示比较结果,例如,与11月相比,12月的收入增加了10%,而1月的收入减少了8%,以此类推。我的目标是在几个月之间显示这些增加,按百分比计算 当前sql查询(不多): 查询工作并显示年度和月份的价格总和 指定的 我如何从这里开始前进,做我上面提到

我想编写一个
sql查询
,以百分比显示月份的收入

例如,我需要计算2016年11月添加的订单的
price
字段条目的
sum
(我得到了一个日期字段,
时间戳
类型)

然后计算从2016年12月到今天(一月二月三月等)的
价格
字段条目的
总和

然后以百分比的形式显示比较结果,例如,与11月相比,12月的收入增加了10%,而1月的收入减少了8%,以此类推。我的目标是在几个月之间显示这些增加,按百分比计算

当前sql查询(不多):

查询工作并显示年度和月份的价格总和 指定的

我如何从这里开始前进,做我上面提到的事情

如果有人给我指点,我将不胜感激! 请让我知道,如果我忘了提到什么或我的问题不清楚,我会编辑

使用
MysQL


表名为
订单
,字段名为
价格
(int)、
日期
(时间戳)和其他(id等)

这将返回按月份分组的订单总数

select  year(date) as year, month(date) as month, sum(price) as total
from    orders
group by year(date), month(date)
显示每个月与前一个月之间的百分比变化需要将上面的查询本身连接起来,如

select  t1.month, t1.year, t1.total, (t1.total / t2.total - 1) * 100 as variation
from    (
            select  year(date) as year, month(date) as month, sum(price) as total
            from    orders
            group by year(date), month(date)
        ) t1
join    (
            select  year(date) as year, month(date) as month, sum(price) as total
            from    orders
            group by year(date), month(date)
        ) t2
on      t2.year = case when t1.month = 1 then t1.year - 1 else t1.year end and
        t2.month = case when t1.month = 1 then 12 else t1.month - 1 end;
编辑

在您的表格中,您只有2016年10月和12月以及2017年2月和3月的数据,因此只有连续两个月是2月/3月;这就是为什么只显示三月

增加的幅度很大,因为每月的总额从不足100美元增加到近1000美元,因此这个百分比也就增加了。我修正了公式,现在应该更精确了

顺便说一句,您可以看到查询使用的是假数据

编辑2

我找到了一种方法,使查询在缺少月份的情况下也能正常工作。联接的逻辑被移动到一个关系表中,该关系表将每个月链接到最近的上一个月

select  t1.year, t1.month, t1.total, (t1.total / t2.total - 1) * 100 as variation
from    (
            /* month results */
            select  year(date) as year, month(date) as month, sum(price) as total
            from    orders
            group by year(date), month(date)
        ) t1
join    (
            /* relationship between months and previous months */
            select  year(t1.date) * 100 + month(t1.date) t1_calc,
                    max(year(t2.date) * 100 + month(t2.date)) t2_calc
            from    orders t1
            join    orders t2
            on      year(t1.date)*100 + month(t1.date) > year(t2.date)*100 + month(t2.date)
            group by month(t1.date)
        ) rel
on      t1.year * 100 + t1.month = rel.t1_calc
join    (
            /* previous month results */
            select  year(date) as year, month(date) as month, sum(price) as total
            from    orders
            group by year(date), month(date)
        ) t2
on      t2.year * 100 + t2.month = rel.t2_calc;

您可以在操作中看到它

如果您在日期为'2016-12'的订单中选择价格,结果会是什么?请尝试在日期为'2016-12-%'的订单中选择价格。@jarlh无任何查询,但不显示任何内容。将like添加到where子句中,返回价格的总和。谢谢可以进一步展开吗?日期列包含年、月和日部分。您希望所有行都具有指定的年和月,但不包括任何一天。太棒了,现在就开始工作。我将编辑这个问题,删除日期部分,因为这已经解决了,谢谢。希望把剩下的事情做完你好,谢谢你的回答。这两个查询的工作,如果我只使用您的第二个查询代码,它只显示2017年3月的价格和8906.6667的变化。我很困惑,它不应该显示每个月之间的变化吗?你能从你的订单表中提供一些样本数据吗?这可能是因为一些打字错误,没有数据很难测试:)当然,为您添加了两个屏幕,一个是您提供的第二个代码的结果,另一个是orders表中一些数据的屏幕。不用担心价格和日期会用另一种语言命名,我已经对它进行了适当的修改,所以这不是问题所在。哦,对不起,我刚意识到你可能指的是真实的数据,而不是屏幕。如果是这样的话,让我知道,我会做一个出口或设置一个粘贴箱或什么:)不客气!我用更好的解决方案编辑了我的答案
select  t1.year, t1.month, t1.total, (t1.total / t2.total - 1) * 100 as variation
from    (
            /* month results */
            select  year(date) as year, month(date) as month, sum(price) as total
            from    orders
            group by year(date), month(date)
        ) t1
join    (
            /* relationship between months and previous months */
            select  year(t1.date) * 100 + month(t1.date) t1_calc,
                    max(year(t2.date) * 100 + month(t2.date)) t2_calc
            from    orders t1
            join    orders t2
            on      year(t1.date)*100 + month(t1.date) > year(t2.date)*100 + month(t2.date)
            group by month(t1.date)
        ) rel
on      t1.year * 100 + t1.month = rel.t1_calc
join    (
            /* previous month results */
            select  year(date) as year, month(date) as month, sum(price) as total
            from    orders
            group by year(date), month(date)
        ) t2
on      t2.year * 100 + t2.month = rel.t2_calc;