Retrieve a list of Ids from a table where all the records have a given status [closed]
题
I have a table with data related to books.
Schema:
BookKey PRIMARY KEY,
BookID,
Status
The primary key of the table is a BookKey
and each BookKey
belongs to one BookId
.
Each row also has a status field. Possible status codes are A
, L
, S
.
I want to build a query to list all BookID
for which all BookKey
have a Status = S
.
解决方案
SELECT BookId
FROM datatable
GROUP BY BookID
HAVING 0 = SUM(BookKey NOT IN ('S'))
不隶属于 dba.stackexchange