2013-05-11 296 views
1

我有写了一个查询的员工是这样显示的租借书籍数量图书馆数据库:SQL嵌套查询

select Emploee.[Emp-No],count(*) as ecount 
from Emploee 
inner join Loan on Emploee.[Emp-No]=Loan.[Emp-No] 
inner join Book on Loan.ISBN=Book.ISBN group by Emploee.[Emp-No] 

上面查询的结果是这样的:

Emp-No ecount 
------------------ 
1000  4 
1001  2 
1002  3 

现在我想修改输出结果,并将每行结果的ecount列与另一个查询结果进行比较,该查询给出了基于该用户发布的特定的借阅图书计数 换言之,我要查找的结果是像这样

Emp-No ecount  
----------------- 
1000  4    

假定员工1000从一个出版商借出了他的所有书。他会在结果中表现出来。

像这样

"..... my query...." having ecount= 
    (select count(*) from books where publisher='A') 

,但我不能使用该结果ecount在另一个查询:(

+3

我不知道理解你的问题。你为什么不在你的第一个查询中添加WHERE Book.Publisher ='XXX''? – Andreas 2013-05-11 15:26:09

+0

对不起,我知道它可能不清楚,但我想找到,如果雇主从'X'以外的出版商获得一本书。 WHERE Book.Publisher ='XXX'给了我特定发布者的数量,我想将它与用户之前获得的所有书籍进行比较 – user1229351 2013-05-11 15:36:30

回答

2

要表示一个集合在另一个查询,汇总都必须有一个别名。SQL Server会因此您需要一个子查询来定义别名,然后才能在另一个子查询中使用别名。

例如,以下SQL使用子查询来定义别名bookcountcount(*)。由于这个第一子查询中,where子句中的第二个子查询可以使用bookcount

declare @books table (title varchar(50), author varchar(50)); 
declare @author_filter table (name varchar(50), bookcount int); 

insert @books values 
    ('The Lord of the Rings', 'J.R.R. Tolkien'), 
    ('The Silmarillion', 'J.R.R. Tolkien'), 
    ('A Song for Arbonne', 'G.G. Kay'); 
insert @author_filter values 
    ('2_books', 2); 

select * 
from (
     select author 
     ,  count(*) as bookcount 
     from @books 
     group by 
       author 
     ) authors 
where '2_books_filter' = 
     (
     select filter.name 
     from @author_filter filter 
     where authors.bookcount = filter.bookcount 
     ) 
+1

这句话“SQL Server不允许您在同一级别引用别名“。使我的一天!谢谢! – user1229351 2013-05-11 16:02:50

5

澄清后,我明白了一个问题,如下:返回只从单一的出版商借出书籍的员工。

您可以在HAVING条款中使用COUNT(DISTINCT publisher)

像这样:

declare @employee table (id int); 
declare @books table (isbn int, title varchar(50), publisher varchar(50)); 
declare @loan table (employee_id int, book_isbn int); 

insert @employee values (1000); 
insert @employee values (1001); 
insert @employee values (1002); 

insert @books values (1, 'Some Book', 'Publisher A'); 
insert @books values (2, 'Some Book', 'Publisher A'); 
insert @books values (3, 'Some Book', 'Publisher A'); 
insert @books values (4, 'Some Book', 'Publisher B'); 
insert @books values (5, 'Some Book', 'Publisher B'); 
insert @books values (6, 'Some Book', 'Publisher B'); 

insert @loan values (1000, 1); 
insert @loan values (1000, 2); 
insert @loan values (1001, 3); 
insert @loan values (1001, 4); 
insert @loan values (1001, 5); 


-- Show the number of different publishers per employee 

select e.id, count(*) as ecount, count(DISTINCT b.publisher) as publishers 
from @employee e 
inner join @loan l on e.id = l.employee_id 
inner join @books b on l.book_isbn = b.isbn 
group by e.id 

-- yields: id   ecount  publishers 
--   ----------- ----------- ----------- 
--   1000  2   1 
--   1001  3   2 



-- Filter on the number of different publishers per employee 

select e.id, count(*) as ecount 
from @employee e 
inner join @loan l on e.id = l.employee_id 
inner join @books b on l.book_isbn = b.isbn 
group by e.id 
having count(DISTINCT b.publisher) = 1 

-- yields: id   ecount 
--   ----------- ----------- 
--   1000  2 
+0

+1好的答案,没有子查询的复杂性 – Andomar 2013-05-11 16:13:53