【发布时间】:2019-04-25 10:40:39
【问题描述】:
我想了解为什么相同的查询会在 Teradata 和 My SQL 中产生不同的结果。 我正在尝试为运行总计编写查询,每个数据库都给了我不同的解决方案。
下面是代码:
CREATE TABLE runn_tot (p_id int, p_name varchar(10), price decimal(5,2));
insert into runn_tot values (1,'p1',34);
insert into runn_tot values (2,'p1',56);
insert into runn_tot values (3,'p1',65);
insert into runn_tot values (4,'p1',12);
insert into runn_tot values (5,'p1',34);
insert into runn_tot values (6,'p1',78);
insert into runn_tot values (7,'p1',23);
insert into runn_tot values (8,'p1',55);
insert into runn_tot values (9,'p1',34);
insert into runn_tot values (10,'p1',66);
我在 MySQL 和 Teradata 中使用的查询
select p_id, p_name, SUM(price) OVER ( partition by p_name order by p_id) Running_Total
from runn_tot;
来自 MySQL 的结果:
+------+--------+---------------+
| p_id | p_name | Running_Total |
+------+--------+---------------+
| 1 | p1 | 34.00 |
| 2 | p1 | 90.00 |
| 3 | p1 | 155.00 |
| 4 | p1 | 167.00 |
| 5 | p1 | 201.00 |
| 6 | p1 | 279.00 |
| 7 | p1 | 302.00 |
| 8 | p1 | 357.00 |
| 9 | p1 | 391.00 |
| 10 | p1 | 457.00 |
+------+--------+---------------+
Teradata 的结果:
1 p1 457.00
2 p1 457.00
3 p1 457.00
4 p1 457.00
5 p1 457.00
6 p1 457.00
7 p1 457.00
8 p1 457.00
9 p1 457.00
10 p1 457.00
我试图理解为什么 MySQL 能够获得正确的运行总计,而 teradata 没有正确执行窗口函数。
【问题讨论】: