无法找出我的“ with”中的语法错误

时间:2019-03-02 19:50:48

标签: sqlite case

这是我的第一篇文章。请原谅我可能犯的任何典型的菜鸟错误! 在下面的代码中,我只是试图输出两列“ Invoice_id”和“ Category”。 第二列是使用case-when条件创建的。 在这种情况下,我正在使用使用“ with”语句创建的两个表。 我只是无法通过“ with”找出语法错误。 任何帮助是极大的赞赏! (我没有遵循标准的SQL小写/大写格式。请忽略)

with get_tracks as
    (
    Select 
        i.invoice_id,
        i.customer_id,
        t.track_id as track_id,
        a.album_id as album_id
    from invoice i
    inner join invoice_line il on i.invoice_id=il.invoice_id
    inner join track t on il.track_id=t.track_id
    inner join album a on a.album_id=t.album_id
    )
with tracks_per_album as
    (
    select 
        count(distinct t.track_id) as num_of_tracks,
        a.album_id as album_id
    from album a 
    inner join track t on a.album_id=t.album_id
    group by 2
    )

select 
    invoice_id,
    case
        when ((select count(distinct track_id) 
        from get_tracks 
        group by album_id
        except
        select num_of_tracks from tracks_per_album
        group by album_id)

        AND

        (select num_of_tracks from tracks_per_album
         group by album_id
         except
         select count(distinct track_id) from get_tracks 
         group by album_id
          ) is Null) then "Album"

          else "Individual"
    end as category
from get_tracks group by 1

错误消息:

DatabaseError: Execution failed on sql '
with get_tracks as
    (
    Select 
        i.invoice_id,
        i.customer_id,
        t.track_id as track_id,
        a.album_id as album_id
    from invoice i
    inner join invoice_line il on i.invoice_id=il.invoice_id
    inner join track t on il.track_id=t.track_id
    inner join album a on a.album_id=t.album_id
    )

with tracks_per_album as
    (
    select 
        count(distinct t.track_id) as num_of_tracks,
        a.album_id as album_id
    from album a 
    inner join track t on a.album_id=t.album_id
    group by 2
    )

select 
    invoice_id,
    case
        when ((select count(distinct track_id) 
        from get_tracks 
        group by album_id
        except
        select num_of_tracks from tracks_per_album
        group by album_id)

        AND

        (select num_of_tracks from tracks_per_album
         group by album_id
         except
         select count(distinct track_id) from get_tracks 
         group by album_id
          ) is Null) then "Album"

          else "Individual"
    end as category
from get_tracks group by 1
': near "with": syntax error

2 个答案:

答案 0 :(得分:0)

Can I use multiple "with"?相关的种类,因为该错误与使用多个withs处理有关。抱歉,不知道这应该是评论还是答案,而保留为答案。

答案 1 :(得分:0)

那是不正确的语法。错误消息有点在编辑中丢失:

from get_tracks group by 1 ': near "with": syntax error

此查询中只需要一个WITH。要使用语句“链接”,请使用以下语法:

with get_tracks as
    (
    Select 
     .  
     .  
     .  
     .
    ),

 tracks_per_album as
    (
    select ..... 

在第一个表的末尾注意,,在创建第二个表之前注意不要with