mysql - Alternative for a loop -
i trying generate simple report display number of customers owning number of distinct brands. following query wrote generates desired numbers 1 @ time. tried writing loop , takes forever. there alternative?
select count(distinct customer_id) ( select customer_id,count(distinct brand) no_of_customers table_a brand_id != 10 group customer_id having count(distinct brand) =1 order customer_id) t1;
what give me count of customers total count of distinct brands =1. change count of brands 2,3 , on. please let me know if there way automate this.
thanks lot.
use second level of group by
them in 1 query, rather looping.
select no_of_brands, count(*) no_of_customers (select customer_id, count(distinct brand) no_of_brands table_a brand_id != 10 group customer_id) x group no_of_brands
you don't need distinct
in outer query, since inner query's grouping guarantees customer ids distinct.
Comments
Post a Comment