Need help with a conditional SELECT statement
Posted
by Ethan
on Stack Overflow
See other posts from Stack Overflow
or by Ethan
Published on 2010-04-29T17:21:56Z
Indexed on
2010/04/29
17:27 UTC
Read the original article
Hit count: 295
I've got a stored procedure with a select statement, like this:
`SELECT author_ID,
author_name,
author_bio
FROM Authors
WHERE author_ID in (SELECT author_ID from Books) `
This limits results to authors who have book records. This is the Books table:
Books
book_ID INT
author_ID INT
book_title NVARCHAR
featured_book BIT
What I want to do is conditionally select the ID of the featured book by each author as part of the select statement above, and if none of the books for a given author are featured, select the ID of the first (top 1) book by the author from the books table. How do I approach this?
© Stack Overflow or respective owner