这是我的表结构
CREATE TABLE branch_book_list (
branch_name CHAR(10),
book_ISBN CHAR (13),
book_name CHAR(40),
PRIMARY KEY (branch_name, book_ISBN)
)
我在表中有这些数据行:
branch_name book_ISBN book_name
----------- ------------- ----------------------------------------
Branch A 1-56592-756-7 Transact-SQL Cookbook
Branch A 1-56592-401-0 Transact-SQL Programming
Branch A 1-56592-401-0 Transact-SQL Programming
Branch B 1-56592-756-7 Transact-SQL Cookbook
Branch B 1-56592-756-7 Transact-SQL Cookbook
Branch B 1-56592-401-0 Transact-SQL Programming
当我执行此查询时:
SELECT bbl1.*, COUNT(*) DupeCount
FROM branch_book_list bbl1
WHERE bbl1.branch_name = 'Branch A'
GROUP BY bbl1.branch_name, bbl1.book_ISBN, bbl1.book_name
HAVING NOT EXISTS (
SELECT bbl2.*, COUNT(*)
FROM branch_book_list bbl2
WHERE branch_name = 'Branch B'
GROUP BY bbl2.branch_name, bbl2.book_ISBN, bbl2.book_name
HAVING bbl1.book_ISBN = bbl2.book_ISBN
AND bbl1.book_name = bbl2.book_name
AND COUNT(*) = COUNT(ALL bbl1.book_ISBN))
输出是
branch_name book_ISBN book_name DupeCount
----------- ------------- ---------------------------------------- -----------
Branch A 1-56592-401-0 Transact-SQL Programming 2
Branch A 1-56592-756-7 Transact-SQL Cookbook 1
当我只执行查询的顶部时
SELECT bbl1.*, COUNT(*) DupeCount
FROM branch_book_list bbl1
WHERE bbl1.branch_name = 'Branch A'
GROUP BY bbl1.branch_name, bbl1.book_ISBN, bbl1.book_name
我的输出是
branch_name book_ISBN book_name DupeCount
Branch A 1-56592-401-0 Transact-SQL Programming 2
Branch A 1-56592-756-7 Transact-SQL Cookbook 1
但是当完整的查询被执行时,我如何得到这个输出
branch_name book_ISBN book_name DupeCount
----------- ------------- ---------------------------------------- -----------
Branch A 1-56592-401-0 Transact-SQL Programming 2
Branch A 1-56592-756-7 Transact-SQL Cookbook 1
谁能告诉我如何获得高于输出?