【问题标题】:Convert Oracle query to SQL Server 2008将 Oracle 查询转换为 SQL Server 2008
【发布时间】:2018-05-02 23:27:22
【问题描述】:

我在将 Oracle 查询转换为其 SQL Server 2008 版本时遇到问题。在此 Oracle 查询中使用了 LISTAGGconnect by prior。我研究了网络,发现WITH AS () 在 SQL Server 2008 中等同于 connect by prior

对于 SQL Server 2008 中的 LISTAGG 备用,我们也可以使用 STUFF()。但是我在将这两种逻辑合二为一时遇到了困难。请帮我。我尝试的解决方案似乎是错误的。

这里是 Oracle 查询:

SELECT
    (LISTAGG(T1.c2, '/') WITHIN GROUP (ORDER BY T1.c1)) 
FROM 
    Table1 T1 
START WITH T1.c1 = T2.c3 
CONNECT BY PRIOR T1.c3 = T1.c1

这是我尝试过的解决方案:

with n(col1, col2) as 
(
    select T1.c1, '/' + T1.c2
    from table1 T1,
    where T1.c1 = T2.c3
    union ALL
    select T3.c1, '/' + T3.c2
    from table1 as T4,T3
    where T4.c3 = T3.c1  
)
select col2 from n;

在这两个查询中,T2 是在外部查询中使用的 Table2 的引用。

例如:

Select 
.....,
.....,
.
.
(
  select (LISTAGG(T1.c2, '/') WITHIN GROUP (ORDER BY T1.c1)) 
  FROM Table1 T1 
  start with T1.c1 = T2.c3 
  connect by prior T1.c3 = T1.c1
) as ABC,
'
'
'
From
Table A,
Table B,
.
.
Table T2

我提到的链接::

【问题讨论】:

    标签: sql sql-server oracle sql-server-2008


    【解决方案1】:

    LISTAGG你可以看here

    With 你可以在 SQL 中这样做:

    select * into #n from
    (
        select T1.c1 col1, '/' + T1.c2 col2
        from table1 T1,
        where T1.c1 = T2.c3
        union ALL
        select T3.c1, '/' + T3.c2
        from table1 as T4,T3
        where T4.c3 = T3.c1  
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      相关资源
      最近更新 更多