在SQL中获取SubQuery错误

时间:2016-11-03 09:04:37

标签: sql-server

  

Msg 102,Level 15,State 1,Line 277       ')'附近的语法不正确。

select 
    Book_Num, 
    Book_Title, 
    Book_SUBJECT, 
    AvgCost, 
    (Book_Cost - AvgCost) as 'Difference'
FROM 
    (
        Select 
            Book_Subject, 
            Avg(book_Cost) as AvgCost 
        from 
            BOOK 
        Group BY 
            BOOK_Subject
    )
;

第277行是集团线。不知道为什么我收到这个错误,是子查询的新内容。

6 个答案:

答案 0 :(得分:2)

您看到的错误是因为您需要命名子查询:

select 
    Book_Num, 
    Book_Title, 
    Book_SUBJECT, 
    AvgCost, 
    (Book_Cost - AvgCost) as 'Difference'
FROM 
    (
        Select 
            Book_Subject, 
            Avg(book_Cost) as AvgCost 
        from 
            BOOK 
        Group BY 
            BOOK_Subject
    ) AS Table1  -- <<< Add a name
;

但在纠正该错误后,您将遇到问题,即您在主查询中选择了不在子查询中的字段(Book_NumBook_Title)。

您需要加入另一个表来获取这些字段:

SELECT 
    Book_Num, 
    Book_Title, 
    Table1.Book_SUBJECT, -- <<< You'll need to qualify this field
    AvgCost, 
    (Book_Cost - AvgCost) as 'Difference'
FROM BOOK LEFT JOIN
    (
        Select 
            Book_Subject, 
            Avg(book_Cost) as AvgCost 
        from 
            BOOK 
        Group BY 
            BOOK_Subject
    ) AS Table1
ON BOOK.Book_SUBJECT = Table1.Book_Subject
;

答案 1 :(得分:0)

SELECT a.book_num, 
       a.book_title, 
       a.book_subject, 
       avgcost, 
       ( book_cost - avgcost ) AS 'Difference' 
FROM   (SELECT book_subject, 
               Avg(book_cost) AS AvgCost, 
               book_num, 
               book_title 
        FROM   book 
        GROUP  BY book_subject, 
                  book_num, 
                  book_title) A; 

答案 2 :(得分:0)

select 
    Book_Num, 
    Book_Title, 
    Book_SUBJECT, 
    AvgCost, 
    (Book_Cost - Avg(Book_Cost) over (partition by BOOK_Subject)) as 'Difference'
FROM 
Book

尝试此查询。它简单得多。不使用任何子查询。

答案 3 :(得分:0)

你可以试试这个

WITH cost 
AS
(
Select 
    Book_Subject, 
    Avg(book_Cost) as AvgCost 
from 
    BOOK 
Group BY 
    BOOK_Subject
)
SELECT bk.book_num
,      bk.book_title
,      bk.book_subject
,      cost.AvgCost
,      (bk.book_cost - cost.AvgCost) as 'Difference'
FROM   BOOK bk
JOIN   cost  ON bk.book_subject = cost.book_subject

答案 4 :(得分:0)

如果没有sample data则很难确定,但我认为您正在尝试返回一份完整的图书清单,其中包含同一主题的其他图书的平均费用。

我的示例使用CTE而不是子查询,但原理是相同的。

/* Returns details on each book,
 * including the average price for the subject.
 */
WITH AvgCost AS
    (   
        -- Returns the average cost for each subject.
        Select 
            Book_Subject, 
            Avg(book_Cost) as AvgCost 
        from 
            BOOK 
        Group BY 
            BOOK_Subject
    )
-- Joins the average to the detail.
SELECT 
    bs.Book_Num, 
    bs.Book_Title, 
    bs.Book_SUBJECT, 
    ac.AvgCost, 
    (bs.Book_Cost - ac.AvgCost) as 'Difference'
FROM
    BOOK_Subject AS bs
        INNER JOIN AvgCost AS ac            ON ac.Book_Subject = bs.Book_Subject

;

另一种方法是使用over clause。这允许您返回内联聚合总计。

-- Using the over clause.
SELECT 
    bs.Book_Num, 
    bs.Book_Title, 
    bs.Book_SUBJECT, 
    bs.book_Cost,
    Avg(bs.book_Cost) OVER (PARTITION BY  Book_Subject)                AS AverageCost
    (Avg(bs.book_Cost) OVER (PARTITION BY  Book_Subject) - ac.AvgCost) AS 'Difference'
FROM
    BOOK_Subject AS bs
;

答案 5 :(得分:-2)

select 
     BOOK.Book_Num,
     BOOK.Book_Title,
     BOOK.Book_SUBJECT,
     BOOK.AvgCost,
     (BOOK.Book_Cost - BOOK.AvgCost) as Difference 
FROM BOOK
where 
(
    Select 
        AVG(BOOK.book_Cost) as AvgCost 
    FROM BOOK 
    where BOOK.book_Cost = 100
) 
Group BY BOOK.BOOK_Subject;