sql >> Database teknologi >  >> RDS >> Mysql

Optælling af samlede rækker og gruppering efter en kolonne i mysql

Først skal du gruppere efter for at få de beregnede kolonner

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

og

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

Så kom tilbage til dit bord

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. (Min)SQL fuld join med tre borde

  2. Arbejde med Java Data i Sisense

  3. MySQLNonTransientConnectionException Kunne ikke oprette forbindelse til databaseserveren. Forårsaget af:java.lang.NullPointerException

  4. Hvordan fremtvinger man store og små bogstaver i tabelnavne?