-1

I am fed up with creating sql query to database table, which contains, let's say just three columns - id, title and status. What I need is write a select which shows total of all titles and their each and every status sum. Would appreciate any ideas.

Database:

ID Title Status
1 Title A Done
2 Title B Done
3 Title A Rejected
4 Title B Done
5 Title A Done
6 Title B Done
7 Title A Rejected
8 Title B Done

What I need is:

Title Done Rejected
Title A 6 2
Title B 8 0

Thanks in advance.

Dainius Java
  • 125
  • 1
  • 4
  • 14
  • 1
    so you want SO to write the SQL for you, because you are fed up with doing it? – pm100 Mar 05 '22 at 17:26
  • Well, as I wrote before (in case you are blind), I "Would appreciate any ideas". So, SO, so if anyone knows what functions or methoss on postgres that would help, I would be thankful. – Dainius Java Mar 06 '22 at 11:49
  • Well I sure am motivated to help now. I was partially asking if what was needed was SQL. Q2, is it always just those 2 statuses, I mean is it a fixed number? – pm100 Mar 06 '22 at 17:48
  • It will be fixes number of statuses. For now 5. I am sure that I would construct some how the query I need if someone would put me on the right path. I mean, maybe I should look at COUNT, SUM, or some inner joins between same table - I've been trying for 4 hours with no luck. Some people gives links to readme, some people shares thougts and some write sql to help. I don't think taht write sql statement to someone who is stuck is very HUGE thing, but I see that there are angry people also... – Dainius Java Mar 06 '22 at 18:04

1 Answers1

1
SELECT Title,
   SUM(CASE WHEN Status='Done' THEN 1 ELSE 0 END)  as Done,
   SUM(CASE WHEN Status='Rejected' THEN 1 ELSE 0 END)  as Rejected
   FROM [Table] group by Title

thanks to

Sql Server equivalent of a COUNTIF aggregate function

pm100
  • 48,078
  • 23
  • 82
  • 145