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

Menghitung total baris dan mengelompokkan berdasarkan kolom di mysql

Pertama, Anda perlu mengelompokkan untuk mendapatkan kolom terhitung

 SELECT Customer, COUNT(*) as total_count, SUM(duration) as total_duration
 FROM yourTable
 GROUP BY  Customer

dan

 SELECT Customer, Error, COUNT(*) as error_count
 FROM yourTable
 GROUP BY  Customer, Error

Kemudian bergabung kembali ke meja Anda

SELECT t1.Customer, 
       t2.total_count, 
       t1.error, 
       t3.error_count, 
       t2.total_duration
FROM yourTable as t1
JOIN (
     SELECT Customer, COUNT(*) as total_count, SUM(duration) as total_duration
     FROM yourTable
     GROUP BY  Customer
     ) as t2
  ON t1.Customer = t2.Customer
JOIN (
     SELECT Customer, Error, COUNT(*) as error_count
     FROM yourTable
     GROUP BY  Customer, Error
) as t3
 ON t1.Customer = t3.Customer
AND t1.Error = t3.Error
GROUP BY t1.Customer
       , t1.Error


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Memasukkan SEKARANG() ke dalam Database dengan Rekaman Aktif CodeIgniter

  2. Cara menggunakan tag sql JSTL

  3. kesalahan menjalankan Apache setelah menginstal xampp

  4. pengaturan sql_mode global di mysql

  5. Dapatkan tipe variabel di MySQL