2014-04-04 5 views
0

이 항목에 문제가 있습니다. 화살표에서 구문 오류가 발생했습니다. 나는 목적에 부질의를 사용하고있다. 나는 '어디에서'와 '있는 것'을 혼합 할 수 있는지 궁금합니다.그룹으로 중첩 된 하위 쿼리

;with books_not_ordered as 
(
    select BK.book_id 
    from bkinfo.books BK 
    where BK.book_id not in 
    (
    select OD.book_id 
    from bkorders.order_details OD 
) 
) 
select AU.author_id, AU.author_name_last 
from bkinfo.authors AU 
where exists 
(
    select BAU.author_id, count(*) as NumBooks 
    from bkinfo.book_authors BAU 
    group by BAU.author_id 
    having count(*) > 1 
    ==>where AU.author_id = BAU.author_id 
    and 
    BAU.book_id in 
    (
    select BK.book_id 
    from bkinfo.books BK 
    where BK.book_id in 
    (
     select cte.book_id 
     from books_not_ordered cte 
    )  
) 
) 
; 
go 

답변

2

절하여 그룹 전에 와서 당신이 "얻을 order_details 테이블에없는 책의 저자하려고한다고 가정

select BAU.author_id, count(*) as NumBooks 
    from bkinfo.book_authors BAU 
    where AU.author_id = BAU.author_id 
    group by BAU.author_id 
    having count(*) > 1 
+0

하지만 BAU.book_id가 그룹화 기준에 없기 때문에 HAVING 오류가 발생합니다. 그러나 Group By에 넣으면 잘못 분류됩니다. –

0

를 가지고,하지만 누가 더 쓴해야하는 한 권 이상의 책 ". 다음 쿼리가 작동합니다.

SELECT AU.author_id, AU.author_name_last 
FROM bkinfo.books BK 
    JOIN bkinfo.book_authors BAU ON BAU.author_id = BK.author_id 
    JOIN bkinfo.authors AU ON BAU.author_id = AU.author_id 
    LEFT OUTER JOIN bkorders.order_details OD ON BK.book_id = OD.book_id 
WHERE OD.book_id IS NULL 
GROUP BY AU.author_id, AU.author_name_last 
HAVING COUNT(BK.Book_id) > 1 
+0

당신은 정확하게 목표를 가지고 있습니다! 그러나 불행히도 하위 쿼리를 사용해야합니다. –