0

这可能是一个简单的语法错误,但我已经看了一个小时,我就是找不到它,也许另一双眼睛可以帮助我指出正确的方向??

select 
    lname +', '+ Fname as "Author Name"
from 
    Author a
join 
    BOOKAUTHOR ba on a.AuthorID = ba.authorid
where 
    ba.isbn =  (select ba.isbn  
                from
                    (select 
                        ROW_NUMBER() over (order by sum(quantity) desc) as "Placement", 
                        b.isbn,
                        sum(QUANTITY) as "Total"
                     from 
                        ORDERITEMS oi
                     join 
                        Books b on b.ISBN = oi.isbn
                     group by 
                        b.ISBN)
                where Placement = 1) --the "WHERE" is giving me an error. 
4

1 回答 1

4

当您的 FROM 子句将派生表指定为表源时,您必须为子查询指定别名:

select lname +', '+ Fname as "Author Name"
from Author a
join BOOKAUTHOR ba on
a.AuthorID = ba.authorid
where ba.isbn = 
       (select q.isbn -- <== notice the "q"
        from
            (select ROW_NUMBER() 
            over (order by sum(quantity) desc) as "Placement", 
            b.isbn,
            sum(QUANTITY) as "Total"
            from ORDERITEMS oi
            join Books b
            on b.ISBN = oi.isbn
            group by b.ISBN) q -- <== notice the "q"
        where Placement = 1)

MSDN 库文档

[ FROM { <table_source> } [ ,...n ] ] 
<table_source> ::= 
{
    ...
    | derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ]
    ...
}
于 2013-10-20T20:35:29.560 回答