SQL:数据值修订

SQL:数据值修订,sql,database,sas,relational-database,data-warehouse,Sql,Database,Sas,Relational Database,Data Warehouse,我正在尝试提取任何在我们的数据库表中有修改的口味计数的冰淇淋口味。最终结果应该如下所示:。 我一直在编写这段代码,它基本上错误地返回了are DB中超过一百万行的所有行,并提供了错误的日期时间戳: create table want as ( with temp_tbl as ( select region,report,customer,date,ice_cream_flavor,flavor_count,Order_status,Change_dt from ice_cream_table

我正在尝试提取任何在我们的数据库表中有修改的口味计数的冰淇淋口味。最终结果应该如下所示:。

我一直在编写这段代码,它基本上错误地返回了are DB中超过一百万行的所有行,并提供了错误的日期时间戳:

create table want as (
with temp_tbl as (
select region,report,customer,date,ice_cream_flavor,flavor_count,Order_status,Change_dt
from ice_cream_table
where customer='12345' and Change_dt > to_date(%STR(%'&last_request%'),'DD-MON-YYYY:HH24:MI:SS') and region='south')

select b.region,b.report, b.customer, b.date, b.ice_cream_flavor, 
a.flavor_count as "Previous Value",
b.flavor_count as "Current Value",
a.Change_dt as "Previous Date" 
from (
select region, report, customer, date, ice_cream_flavor, flavor_count
from temp_tbl
where flavor_status='ACTIVE'
order by ice_cream_flavor

) b inner join 
(
select region, report, customer, date, ice_cream_flavor, flavor_count, max(Change_dt) as Change_dt
from temp_tbl
where (flavor_status='Deleted' or flavor_status='Discontinued')
group by region, report, customer, date, ice_cream_flavor, flavor_count
order by ice_cream_flavor) a

on b.ice_cream_flavor=a.ice_cream_flavor
where b.flavor_count<>a.flavor_count /*doing this to provide only changes, not all ice_cream_flavors */
);
quit;
我们的数据是递增的,这意味着修订成为“不连续”行,最新的冰淇淋计数为“活动”


有没有更好的方法来做到这一点,或者我只是在代码中遗漏了一些明显的东西?抱歉发了这么长的帖子

您使用的数据库管理系统和版本是什么?hi@Kateract,使用Oracle数据仓库,与Oracle Ora客户端11g连接。我将从子查询中删除order by,并将删除L仅为主查询保留一个。可以避免一些性能问题