Hacker News new | past | comments | ask | show | jobs | submit login

"I have a table called `onboardings` with the state field. I want to return how many people we have in each state. The Postgres query should return the state, how many people count, and what percentage do those people represent."

Claude-2, correct response and rounds the percentage - makes a nice assumption for me.:

    SELECT state, 
           count(*) AS people_count,  
           round(100.0 * count(*) / (SELECT count(*) FROM onboardings), 2) AS percent
    FROM onboardings 
    GROUP BY state
    ORDER BY people_count DESC;
Phind, correct response as well! Really fast too!:

    WITH state_counts AS (
     SELECT state, COUNT(*) as count
     FROM onboardings
     GROUP BY state
    ),
    total_counts AS (
     SELECT COUNT(*) as total
     FROM onboardings
    )
    SELECT sc.state, sc.count, (sc.count::decimal / tc.total::decimal) * 100 as percentage
    FROM state_counts sc, total_counts tc
    ORDER BY sc.count DESC;



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: