Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Two Select Statements in one Query

I’m new to SQL and I’m having trouble with a pretty basic problem. I have a with column: "primaryProfession" in this column there are actors and actresses. I want to return a result that shows how many Actors there are total and how many Actresses with one query.

This is my code:

SELECT (SELECT COUNT(primaryProfession), 
        FROM title      
        WHERE primaryProfession = "Actor" ) AS "Actors",
        (SELECT COUNT(primaryProfession), 
        FROM title
        WHERE primaryProfession = "Actress") AS "Actresses" 
FROM title;

Any help is appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want it to produce two columns: "Actors" and "Actresses" and show me the total number of each.

>Solution :

You may use conditional aggregation here:

SELECT
    SUM(primaryProfession = 'Actor') AS Actors,
    SUM(primaryProfession = 'Actress') AS Actresses
FROM title;

This approach is succinct and also only requires SQLite to make a single pass over the title table.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading