Hi, I have this SQL code for table with data - CREATE TABLE "users" ( user_id INTEGER, male INTEGER ); INSERT INTO "users" (user_id, male) VALUES ('4050', '1'), ('6254', '0'), ('8260', ''), ('4050', '1'); /* SEX REPRESENTS man - 1 /woman - 0 */ **How do I get % of known male? It is 3/4 - 75%. But what SQL code to get a table with the percentage?** Thanks!
I'm not sure what format you need but here's one way to get what you ask for and a bit more. WITH ctePreAgg AS ( SELECT Male = SUM(male) ,Total = COUNT(*) FROM dbo.users ) SELECT Male ,MalePct = CONVERT(DECIMAL(4,1),Male*100.0/Total) ,Female = Total-Male ,FemalePct = CONVERT(DECIMAL(4,1),(Total-Male)*100.0/Total) ,Total FROM ctePreAgg ;