Retrieve a list of Ids from a table where all the records have a given status [closed]

dba.stackexchange https://dba.stackexchange.com/questions/225343

  •  18-01-2021
  •  | 
  •  

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'))
许可以下: CC-BY-SA归因
不隶属于 dba.stackexchange
scroll top