微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WITH 子句语法错误 - 使用外部选择时

如何解决WITH 子句语法错误 - 使用外部选择时

我在下面创建了一个 sql,当我试图将它放在外部选择下时,它只会显示从 WITH 子句查询下面呈现的结果,它在 sql server 中给我错误,在那里工作正常与甲骨文。 我不想创建一个视图,我想把它放在合并语句中的输出下面...

select custno,slipno,category,donation,unique_nm from
( 
-- Here it is throwing error
with  got_debit_custno (custno,debit_custno)  as
(
    select  custno,case
          when category = 'CREDIT'
          then 'N/A'
          else custno
        end
    from    a
),prep (custno,debit_custno,rn,rdt) as
(
    select  dc.*,row_number ()  over (partition by debit_custno,category order by donation,slipno),sum (donation) over (partition by debit_custno,slipno)
    from    got_debit_custno dc
),r (custno,rdt,mn) as
(
    select  custno,case
            when rn = max(rn) over (partition by debit_custno,category)
            then rn
        end,1
    from    prep
    where   rdt <= 27000 or rn = 1
 union all
    select  p.custno,p.slipno,p.category,p.donation,p.debit_custno,case
            when p.rn = max(p.rn) over (partition by p.debit_custno,p.category)
            then p.rn
        end,p.rdt,r.mn + 1
    from    prep p
    join    r    on  p.debit_custno = r.debit_custno
             and p.category = r.categoRy
             and p.rn > r.rn
                         and (p.rdt <= r.rdt + 27000 or p.rn = r.rn + 1)
)
select    custno,dense_rank () over (order by debit_custno,mn) as unique_nm
from      r)
order by  custno,unique_nm,donation

解决方法

WITH 必须是查询的第一个关键字。前面的语句必须以 ; 终止:

    ;WITH got_debit_custno (custno,slipno,category,donation,debit_custno)  as
    (
        select  custno,case
              when category = 'CREDIT'
              then 'N/A'
              else custno
            end
        from    a
    ),prep (custno,debit_custno,rn,rdt) as
    (
        select  dc.*,row_number ()  over (partition by debit_custno,category order by donation,slipno),sum (donation) over (partition by debit_custno,slipno)
        from    got_debit_custno dc
    ),r (custno,rdt,mn) as
    (
        select  custno,case
                when rn = max(rn) over (partition by debit_custno,category)
                then rn
            end,1
        from    prep
        where   rdt <= 27000 or rn = 1
     union all
        select  p.custno,p.slipno,p.category,p.donation,p.debit_custno,case
                when p.rn = max(p.rn) over (partition by p.debit_custno,p.category)
                then p.rn
            end,p.rdt,r.mn + 1
        from    prep p
        join    r    on  p.debit_custno = r.debit_custno
                 and p.category = r.categoRy
                 and p.rn > r.rn
                             and (p.rdt <= r.rdt + 27000 or p.rn = r.rn + 1)
    )
    select    custno,dense_rank () over (order by debit_custno,mn) as unique_nm
    from      r
    order by  custno,unique_nm,donation

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。