我正在尝试做一些数据报告,并且不经常做SQL体操以了解我正在寻找什么功能。我称之为“ungroup by”。我有一个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的缓慢计算)。
答案 0 :(得分: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。
祝你好运。答案 1 :(得分: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
完成!
答案 2 :(得分: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 |