sql >> Database >  >> RDS >> PostgreSQL

Postgres COUNT aantal kolomwaarden met INNER JOIN

Je moet proberen:

SELECT a.question_id, 
       SUM(CASE WHEN a.response = 'Yes' THEN 1 ELSE 0 END) AS NumsOfYes, 
       SUM(CASE WHEN a.response = 'No' THEN 1 ELSE 0 END) AS NumsOfNo,
       SUM(CASE WHEN a.response = 'Possibly' THEN 1 ELSE 0 END) AS NumOfPossibly,
       costperlead  * SUM(CASE WHEN a.response = 'Yes' THEN 1 ELSE 0 END) + SUM(CASE WHEN a.response = 'Possibly' THEN 1 ELSE 0 END) AS revenue
FROM responses a 
     INNER JOIN questions b ON a.question_id = b.id 
GROUP BY a.question_id, b.costperlead


  1. Verschil tussen ON- en WHERE-clausules in SQL-tabeljoins

  2. Evolutie van fouttolerantie in PostgreSQL:replicatiefase

  3. DML-statements testen voor In-Memory OLTP

  4. PHP-weergave afbeelding BLOB van MySQL