1

i have a library database which wrote a query to display count of loaned books by employee like this:

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] 

the result of above query is something like this:

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

now i want to modify the output and make a comparison between ecount column of each row of result with another query which give me count of loaned books based on specific published by that user in other word the result im looking for is something like this

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

assume Employee 1000 loaned all of his book from one publisher. he will be showen in the result.

something like this

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

but i cant use the result ecount in another query :(

Andomar
  • 232,371
  • 49
  • 380
  • 404
user1229351
  • 1,985
  • 4
  • 20
  • 24
  • 3
    I'm not sure to understand your question. Why don't you just add `WHERE Book.Publisher = 'XXX'` in your first query? – Andreas May 11 '13 at 15:26
  • sorry i know that it might be unclear but i want to find that if an employ get a book from a publisher other than 'X'. WHERE Book.Publisher = 'XXX' is giving me count of the specific publisher and i want to compare it with all of book that a user got before – user1229351 May 11 '13 at 15:36

2 Answers2

5

After clarification, I understood the question as follows: return those employees which only loaned books from a single publisher.

You can do that by using COUNT(DISTINCT publisher) in your HAVING clause.

Like so:

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
Andreas
  • 1,751
  • 2
  • 14
  • 25
2

To refer to an aggregate in another query, the aggregate has to have an alias. SQL Server does not allow you to refer to an alias at the same level. So you need a subquery to define the alias, before you can use the alias in another subquery.

For example, the following SQL uses a subquery to define alias bookcount for count(*). Thanks to this first subquery, the second subquery in the where clause can use the 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
        )
Andomar
  • 232,371
  • 49
  • 380
  • 404