Task. Show count of returned and not returned books in the library.

We should make "y/n" mark convertation to 'Returned'/'Not returned' string literal. The solution for MS SQL include second convertation because of it not support use naming of calculated expression.

SELECT (CASE
            WHEN [sb_is_active] = 'Y'
            THEN 'Not returned'
            ELSE 'Returned'
        END)           AS [status],
        COUNT([sb_id]) AS [books]
FROM [subscriptions]
GROUP BY (CASE
            WHEN [sb_is_active] = 'Y'
            THEN 'Not returned'
            ELSE 'Returned'
            END)
ORDER BY [status] DESC

Комментарии

Популярные сообщения из этого блога

MapBox-compatible file formats and limitations