1

我有一个图书馆数据库,它编写了一个查询来显示员工借出的书籍数量,如下所示:

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 :(

4

2 回答 2

5

澄清后,我对问题的理解是:归还那些只从单一出版商那里借书的员工。

您可以通过COUNT(DISTINCT publisher)在您的HAVING子句中使用来做到这一点。

像这样:

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
于 2013-05-11T16:02:59.510 回答
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
        )
于 2013-05-11T15:45:22.047 回答