且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将SQL Server CTE转换为Oracle CTE

更新时间:2023-02-02 22:46:35

假定您在cte/cte3选择列表中没有故意反转m和y列,我认为您可以将查询重写为: >

Assuming you didn't have the m and y columns reversed on purpose in your cte/cte3 select lists, I think you could rewrite your query as:

with cte1 as (select a.ac,
                     a.m,
                     a.y,
                     a.d,
                     a.e,
                     a.f,
                     a.cd,
                     max(case when a.cd is not null and b.cd is not null then a.y end) over (partition by a.ac) max_y,
                     max(case when a.cd is not null and b.cd is not null then a.m end) over (partition by a.ac) max_m,
                     max(case when a.cd is not null and b.cd is not null then a.cd end) over (partition by a.ac) max_cd
              from   tbla a
                     left outer join tblb b on (a.ac = b.ac))
select ac,
       m,
       y,
       d,
       e,
       f,
       cd
from   cte1
where  (y = to_char(sysdate, 'yyyy')
        and m = to_char(add_months(sysdate, -1), 'mm'))
or     (y = max_y
        and m = max_m
        and cd = max_cd);

您没有提供任何示例数据,因此我无法进行测试,但是值得将date函数转换为其等效的SQL Server,并进行测试以确保返回的数据相同.

You haven't provided any sample data, so I can't test, but it would be worth converting the date functions to their SQL Server equivalents and testing to make sure the data returned is the same.

这样,您不会查询同一张表3次,这应该会提高性能.

This way, you're not querying the same table 3 times, which should improve the performance some.