我编写了下面的查询,作为创建表的更大查询的一部分。因为我是SQL的新手,所以这是以一种非常循序渐进的方式完成的,因此我可以很容易地理解查询中的各个步骤以及每个部分都在做什么。
然而,我现在的任务是通过将查询的以下两个部分连接在一起来提高它们的效率,这就是我正在努力的地方。
我觉得我应该创建一个表而不是2个表,并且这个表应该包含我需要的所有列/值。但是,我完全不确定执行此操作所需的语法,也不确定重写查询所需的顺序。
有没有人能给点建议?
非常感谢
sys_type as (select nvl(dw_start_date,sysdate) date_updated, id, descr
from scd2.scd2_table_a
inner join year_month_period
on 1=1
WHERE batch_end_date BETWEEN dw_start_date and NVL(dw_end_date,sysdate)),
sys_type_2 as (select -1 as sys_typ_id,
'Unassigned' as sys_typ_desc,
sysdate as date_updated
from dual
union
select id as sys_typ_id, descr as sys_typ_desc, date_updated
from sys_type),发布于 2017-09-06 17:06:47
假设您使用的是Oracle数据库,上面的查询看起来很好。我不认为你可以仅仅通过‘加入’他们(这里定义非常宽松的连接)来使他们更有效率。是否存在性能问题?
我认为你可以通过调整你的第一个内联查询'sys_type‘来获得更好的结果。
你有一个笛卡尔乘积。你需要这个吗?为什么不把where子句中的条件作为join子句呢?基本上
sys_type as (select nvl(dw_start_date,sysdate) date_updated, id, descr
from scd2.scd2_table_a
inner join year_month_period
on (batch_end_date BETWEEN dw_start_date and NVL(dw_end_date,sysdate)))https://stackoverflow.com/questions/46069990
复制相似问题