4

我正在尝试做一些数据报告,并且不要经常做 SQL 体操来知道我在寻找什么功能。我将其称为“取消分组依据”。我有一个 SELECT 输出这个,反映了一系列每月订阅项目:它们何时创建、关闭以及每月支出是多少:

+----+---------------------------------+------------+------------+------------+
| id | description                     | created_on | closed_on  | monthly    |
+----+---------------------------------+------------+------------+------------+
|  3 | Daily horoscope email           | 2012-01-01 | null       | 10000.0000 |
|  5 | Pet food delivery               | 2012-01-05 | null       |  3500.0000 |
|  6 | Dirty magazine subscription     | 2012-01-09 | null       |  1500.0000 |
|  7 | Stupid nuts posted in a box     | 2012-01-01 | 2012-01-04 |  1500.0000 |
  .... etc ...

我正在尝试做的是每天计算“运行率”。因此,每天都会列出当前每月承诺的总计,即上述数据将映射到:

+------------+----------+
| date       | run_rate |
+------------+----------+
| 2012-01-01 | 11500    |
| 2012-01-02 | 11500    |
| 2012-01-03 | 11500    |
| 2012-01-04 | 10000    |
| 2012-01-05 | 13500    |
| 2012-01-06 | 13500    |
| 2012-01-07 | 13500    |
| 2012-01-08 | 13500    |
| 2012-01-09 | 15000    |

我认为可能的是创建一个每天有一行的临时表,然后编写一个引用第一个表的 LEFT JOIN / GROUP BY 语句来构建我的输出。但是我只能看到如何以这种方式创建每天的“差异”,而不是运行总计,并且我需要将第一个表“取消组合”为两个条目,这是创建订阅时的正条目,以及关闭时的否定条目。

我想坚持 MySQL,如果可能的话,在一个大型声明中。如果这不可能,我可以将一些存储过程或临时表添加到我的查询框架中。或者我真的必须通过 Ruby 来研究我的数据吗?(我确切地知道怎么做,但希望我能把所有的逻辑都放在一个地方,我正在努力改进我们目前使用 ActiveRecord 的缓慢计算)。

4

3 回答 3

1

尝试这样的事情——应该会产生你想要的结果:

SET @runtot:=0;
SELECT
   mydates.seeddate,
    (@runtot := @runtot + IFNULL(m.amt,0) - IFNULL(m2.amt,0)) AS rt
FROM
   mydates left join 
    (Select createdon, SUM(monthly) amt
     FROM mytable 
     group by createdon
     ) m on mydates.seeddate = m.createdon
left join 
    (Select closed_on, SUM(monthly) amt
     FROM mytable 
     group by closed_on  
     ) m2 on mydates.seeddate = m2.closed_on

这是SQL Fiddle

祝你好运。

于 2013-01-19T03:38:28.683 回答
1

试试这个:

select date,sum(monthly)
from     
(
   select created_on as date from yourtable 
   union 
   select closed_on from yourtable where closed_on is not null
) as alldates
left outer join yourtable
  on date >= created_on
 and (closed_on is null or date < closed_on)
where date between '2012-1-1' and '2012-1-31'
group by date order by 1

根据您的示例数据,输出为:

+------------+--------------+
| date       | sum(monthly) |
+------------+--------------+
| 2012-01-01 |     11500.00 |
| 2012-01-04 |     10000.00 |
| 2012-01-05 |     13500.00 |
| 2012-01-09 |     15000.00 |
+------------+--------------+
4 rows in set (0.00 sec)

我们可以计算出当天不存在的日期等于最接近的日期。例如,“2012-01-02”的 run_rate 等于“2012-01-01”的 run_rate。

假设您已经有一个包含当月所有日期的表,我们称之为“mydate”,其中有一列“date”。

mysql> select * from mydate where date >= '2012-1-1' and date <= '2012-1-31';
+------------+
| date       |
+------------+
| 2012-01-01 |
| 2012-01-02 |
| 2012-01-03 |
| 2012-01-04 |
| 2012-01-05 |
| 2012-01-06 |
| 2012-01-07 |
...

然后更换

(
select created_on as date from yourtable 
union 
select closed_on from yourtable where closed_on is not null
) as alldates

mydate

完毕!

于 2013-01-19T05:48:38.020 回答
1

这是另一种使用方法:INFORMATION_SCHEMA.COLLATION_CHARACTER_SET_APPLICABILITY它将在其中创建日期,而无需创建临时表。Explan plan可以确认哪种方法最适合您。

SET @rrate:=0;

SELECT X.rdate, (@rrate:=@rrate +
COALESCE(Y.summonthly,0) -
COALESCE(Z.summonthly,0)) as run_rate
FROM(
      SELECT date_add(P.createdon, interval `day` day)
      as rdate
      FROM 
          (SELECT @i:= @i + 1 AS `day`
           FROM   
           INFORMATION_SCHEMA.COLLATION_CHARACTER_SET_APPLICABILITY,
           (SELECT @i:= -1) AS i
           ) As D,
      rategroups P
      GROUP BY rdate
      HAVING rdate <= (SELECT MAX(createdon) FROM rategroups)
      ORDER BY rdate) X
LEFT JOIN 
      (SELECT createdon, sum(monthly) summonthly
       FROM rategroups
       GROUP BY createdon) Y
ON X.rdate = Y.createdon
LEFT JOIN 
      (SELECT closed_on, sum(monthly) summonthly
       FROM rategroups
       GROUP BY closed_on) Z
ON X.rdate = Z.closed_on
GROUP BY X.rdate
;

|                          RDATE | RUN_RATE |
---------------------------------------------
| January, 01 2012 00:00:00+0000 |    11500 |
| January, 02 2012 00:00:00+0000 |    11500 |
| January, 03 2012 00:00:00+0000 |    11500 |
| January, 04 2012 00:00:00+0000 |    10000 |
| January, 05 2012 00:00:00+0000 |    13500 |
| January, 06 2012 00:00:00+0000 |    13500 |
| January, 07 2012 00:00:00+0000 |    13500 |
| January, 08 2012 00:00:00+0000 |    13500 |
| January, 09 2012 00:00:00+0000 |    15000 |
于 2013-01-19T06:25:04.067 回答