Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

hitung beberapa kolom dalam satu kueri

Anda dapat UNION ALL kueri terpisah dan terapkan bersyarat agregasi pada set yang dihasilkan:

select score,
       max(case when type = 'satisfaction' then count end) as satisfaction_count,
       max(case when type = 'response' then count end) as response_count,
       max(case when type = 'responder' then count end) as responder_count,
       max(case when type = 'service' then count end) as service_count             
from (
  select satisfaction_score as score, 
         count(satisfaction_score) as count, 
         'satisfaction' as type
  from j_survey_response
  where satisfaction_score != 0
  group by satisfaction_score

  union all

  select response_score,
         count(response_score) as count, 'response' as type
  from j_survey_response
  where response_score != 0
  group by response_score

  union all

  select responder_score,
         count(responder_score) as count, 'responder' as type
  from j_survey_response
  where responder_score != 0
  group by responder_score

  union all

  select service_score,
         count(service_score) as count, 'service' as type
  from j_survey_response
  where service_score != 0
  group by service_score) as t
group by score


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Apakah ada cara untuk mengembalikan setelah komit di MySQL?

  2. Mysql - Mysql2::Error:Nilai string salah:

  3. Kolom MySQL disetel ke NOT NULL tetapi masih mengizinkan nilai NULL

  4. instal mysql dan mysqlnd di ubuntu 12.04

  5. Pencarian Php Mysql Antara Dua Tanggal