查找同一SQL表中不同值的百分比差异?

时间:2014-11-13 13:30:13

标签: mysql

我有一张表格,用于跟踪特定地点的月份与年份的总值。

期望的结果:我想比较本年度的一个月的价值与去年的价值。然后我想检查一个百分比的增加。

e.g。 2014年(1月)= 140 - 2013年(1月)= 150 * 100 = - 6.67

表名 - donation_tracker

enter image description here

提前谢谢。

3 个答案:

答案 0 :(得分:4)

根据我的理解,您希望从特定地点获得相同月份的去年增加到当年的百分比。使用查询。

SELECT D1.month, ROUND((D2.Donation_amount- D1.Donation_amount) * 100 / 
       D1.Donation_amount, 2)
FROM       donation_tracker D1
INNER JOIN donation_tracker D2 
ON d1.month = D2.month AND D1.year = D2.year - 1 
                       AND D1.Location_ID = D2.Location_ID;

答案 1 :(得分:1)

这应该给你一个想法:)

示例数据:

CREATE TABLE t
    (`month` varchar(3), `year` int, `amount` int)
;

INSERT INTO t
    (`month`, `year`, `amount`)
VALUES
    ('jan', 2013, 150),
    ('feb', 2013, 180),
    ('jan', 2014, 140),
    ('feb', 2014, 160)
;

查询:

select
t1.month, round((t2.amount - t1.amount) * 100 / t1.amount, 2)
from
t t1
inner join t t2 on t1.month = t2.month and t1.year < t2.year;

结果:

| MONTH | ROUND((T2.AMOUNT - T1.AMOUNT) * 100 / T1.AMOUNT, 2) |
|-------|-----------------------------------------------------|
|   jan |                                               -6.67 |
|   feb |                                              -11.11 |

答案 2 :(得分:1)

假设您需要将之前完成的12个月与之前的12个月进行比较,逐月进行比较。我猜你的桌子和列名,因为,我不知道他们。

让我们从头开始构建这个。

这是一个查询,可以逐月查找最近12个月的捐款。

SELECT YEAR(donation_date) AS donation_year, 
       MONTH(donation_date) AS donation_month,
       SUM(donation_amount) AS donation_amount
  FROM donations
 WHERE donation_date >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
   AND donation_date <  LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
 GROUP BY YEAR(donation_date), MONTH(donation_date) 

这会给你一个十二行的结果集(当NOW()恰好在2014年11月中旬时):

2013  11    145
2013  12    220
2014   1    123
2014   2     11
  ...
2014  10     45

诀窍是选择正确范围的donation_date值。

所以,现在你需要两个结果集,一个主要是2014年,一个主要是2013年。大多数2013年的那个看起来非常相似。你只需要再支持一年。

SELECT YEAR(donation_date) AS donation_year, 
       MONTH(donation_date) AS donation_month,
       SUM(donation_amount) AS donation_amount
  FROM donations
 WHERE donation_date >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 25 MONTH
   AND donation_date <  LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
 GROUP BY YEAR(donation_date), MONTH(donation_date) 

这将是由这两个基本查询构成的臭名昭着的俱乐部三明治查询之一。你按月加入它们,然后在SELECT子句中进行百分比计算。

SELECT a.donation_month, 
       a.donation_amount AS this_year, 
       b.donation_amount AS last_year,
       100.0 * (a.donation_amount - b.donation_amount) / b.donation_amount as pct_increase
  FROM (
          /* this year's query */
       ) AS a 
  JOIN (
          /* last year's query */
       ) AS b ON a.donation_month = b.donation_month 
  ORDER BY a.donation_year, a.donation_month

这是整个俱乐部三明治供您的服务器咀嚼。美味!

SELECT a.donation_month, 
       a.donation_amount AS this_year, 
       b.donation_amount AS last_year,
       100.0 * (a.donation_amount - b.donation_amount) / b.donation_amount as pct_increase
  FROM (
        SELECT YEAR(donation_date) AS donation_year, 
               MONTH(donation_date) AS donation_month,
               SUM(donation_amount) AS donation_amount
          FROM donations
         WHERE donation_date >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
           AND donation_date <  LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL  1 MONTH
         GROUP BY YEAR(donation_date), MONTH(donation_date) 
       ) AS a 
  JOIN (
        SELECT YEAR(donation_date) AS donation_year, 
               MONTH(donation_date) AS donation_month,
               SUM(donation_amount) AS donation_amount
          FROM donations
         WHERE donation_date >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 25 MONTH
           AND donation_date <  LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
         GROUP BY YEAR(donation_date), MONTH(donation_date) 
       ) AS b ON a.donation_month = b.donation_month 
  ORDER BY a.donation_year, a.donation_month

一旦你整个俱乐部三明治堆积起来,它看起来很复杂。但它实际上是一堆简单的子查询。