mercredi 6 juillet 2016

Optimize/simplify this SQL query

I have this query and it works, but I know this can be simplified:

SELECT class, COUNT(class), publisher FROM books WHERE publisher = 'Company ABC' GROUP BY class
UNION
SELECT class, COUNT(class), publisher FROM books WHERE publisher = 'Company DEF' GROUP BY class
UNION
SELECT class, COUNT(class), publisher FROM books WHERE publisher = 'Company GHI' GROUP BY class
UNION
SELECT class, COUNT(class), publisher FROM books WHERE publisher = 'Company JKL' GROUP BY class
UNION
SELECT class, COUNT(class), publisher FROM books WHERE publisher = 'Company MNO' GROUP BY class;

It also hard-coded but I really want for the query to look for distinct companies in the table:

SELECT DISTINCT publisher FROM books;

Result:

publisher
Company ABC
Company DEF
Company GHI
Company JKL
Company MNO

Aucun commentaire:

Enregistrer un commentaire