我正在编写一个查询以从数据转储中获取 SCD 2 类型的数据。我的数据和代码如下:
create table promotions
(
start_date date,
end_date date,
promotion_name varchar(50));
Run Code Online (Sandbox Code Playgroud)
插入语句来填充表:
insert into promotions values ('9/1/2017','9/2/2017','P1');
insert into promotions values ('9/2/2017','9/3/2017','P1');
insert into promotions values ('9/3/2017','9/4/2017','P1');
insert into promotions values ('9/4/2017','9/5/2017','P1');
insert into promotions values ('9/5/2017','9/6/2017','P2');
insert into promotions values ('9/6/2017','9/7/2017','P2');
insert into promotions values ('9/7/2017','9/8/2017','P2');
insert into promotions values ('9/8/2017','9/9/2017','P2');
insert into promotions values ('9/9/2017','9/10/2017','P2');
insert into promotions values ('9/10/2017','9/11/2017','P2');
insert into promotions values ('9/11/2017','9/12/2017','P3');
insert into promotions values ('9/12/2017','9/13/2017','P3');
insert into promotions values ('9/13/2017','9/14/2017','P3'); …Run Code Online (Sandbox Code Playgroud) 我想了解为什么同一个查询在Teradata和My SQL中产生不同的结果.我正在尝试编写一个运行总计的查询,每个DB都给了我不同的解决方案.
以下是代码:
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);
Run Code Online (Sandbox Code Playgroud)
我在MySQL和Teradata中使用的查询
select p_id, p_name, SUM(price) OVER ( partition by p_name order by p_id) Running_Total
from runn_tot;
Run Code Online (Sandbox Code Playgroud)
MySQL的结果:
+------+--------+---------------+ …Run Code Online (Sandbox Code Playgroud)