【问题标题】:Sql query to display earnings increase or decrease - in percentagesSql 查询以百分比显示收入增加或减少
【发布时间】:2017-03-30 20:32:20
【问题描述】:

我想写一个sql query,以百分比显示一个月份收入与另一个比较。

例如,对于 2016 年 11 月 中添加的订单,我需要计算 sumprice 字段条目(我有一个 日期 字段, timestamp 类型)

然后计算从 2016 年 12 月等到今天(1 月2 月、三月..等)

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

当前sql查询(不多):

SELECT SUM(price) AS Earnings FROM orders WHERE date LIKE '2016-12-%'

查询有效并且正在显示年份和月份的价格总和 指定。

我如何从这里继续前进并做我上面提到的事情?

我会很感激任何指点! 请让我知道如果我忘记提及某些内容或对我的问题不清楚,我会进行编辑。

使用MysQL

表名称为orders,字段名称为price(int)、date(时间戳)和其他(id等)

【问题讨论】:

  • 如果你这样做select price FROM orders WHERE date='2016-12'会有什么结果?
  • 试试where date like '2016-12-%'
  • @jarlh 什么都没有,查询有效,但不显示任何内容。将 like 添加到 where 子句有效,返回价格总和。谢谢!你能进一步扩展吗?
  • 日期列包含年部分、月部分和日部分。您希望所有行都具有指定的年份和月份,但任何一天。
  • 太棒了,正在工作。解决问题后,我将编辑问题以删除日期部分,谢谢。希望能完成剩下的工作..

标签: mysql sql


【解决方案1】:

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

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,因此是这个百分比。不过我修正了公式,现在应该更精确了。

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

编辑 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;

你可以看到它在行动here

【讨论】:

  • 您好,感谢您的精彩回答。两个查询的工作,如果我只使用您的第二个查询代码,它只会显示 2017 年 3 月的价格总和和 8906.6667 作为变体。我很困惑,它不应该显示每个可用月之间的差异吗?
  • 能否请您提供一些来自您的订单表的示例数据?可能是因为一些拼写错误,没有数据很难测试:)
  • 当然,为您添加了两个屏幕,一个是您提供的第二个代码的结果,另一个是订单表中一些数据的屏幕。不要担心价格和日期会用另一种语言命名,我已经对其进行了适当的修改,所以这不是问题。链接:postimg.org/gallery/xwe0oqbm
  • 哦,对不起,刚刚意识到您可能指的是实际数据,而不是屏幕截图。如果是这种情况,请告诉我,我会做一个导出或设置一个 pastebin 或其他东西:)
  • 不客气!我用更好的解决方案编辑了我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
  • 2012-02-18
  • 2020-10-06
  • 2015-08-13
  • 1970-01-01
  • 2018-07-30
相关资源
最近更新 更多