sql >> Databasteknik >  >> RDS >> PostgreSQL

Postgres COUNT antal kolumnvärden med INNER JOIN

Du bör prova:

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. Hur ändrar jag standardspråk för SQL Server?

  2. Hur man klona R12.2 miljö

  3. ORA-4031-fel med Direct NFS

  4. MySQL - Hur söker man efter exakt ordmatchning med LIKE?