Counting total rows and grouping by a column in mysql -
so i've been looking @ 1 while , can't seem figure out.
i have mysql table following format , sample data:
id, customer, time, error code, duration 1,test1,00:12:00,400,120 2,test2,00:14:00,404,60 3,test1,00:15:00,404,120 4,test2,00:17:00,503,120 5,test1,00:19:00,400,60 6,test1,00:20:00,400,60 7,test2,00:21:00,503,60
i need results have customer name, total number of rows per customer name, error codes per customer, number of errors per customer , sum duration. above results want get:
test1, 4, 400, 3, 360 test1, 4, 404, 1, 360 test2, 2, 404, 1, 240 test2, 2, 503, 2, 240
the problem having when group error code, count number of rows , sum of duration grouped error code. need total count of rows , duration customer not total count of rows , duration customer per error code.
please let me know if need include else or if i'm super confusing am.
thank in advance help!
first need group calculated columns
select customer, count(*) total_count, sum(duration) total_duration yourtable group customer
and
select customer, error, count(*) error_count yourtable group customer, error
then join table
select t1.customer, t2.total_count, t1.error, t3.error_count, t2.total_duration yourtable t1 join ( select customer, count(*) total_count, sum(duration) total_duration yourtable group customer ) t2 on t1.customer = t2.customer join ( select customer, error, count(*) error_count yourtable group customer, error ) t3 on t1.customer = t3.customer , t1.error = t3.error group t1.customer , t1.error
wiki
Comments
Post a Comment