Re: [Skunkworks] PHP/MySQL: Is there a better way to count distinct values in a column

Hie i had the same issue on Monday but i used a view to kill it see if it might help
1) Create a View (my_table1 - 5 ) which pulls all the data you want in the World. Yote Yote
Hmm, I am afraid I don't see the point of your view in this case. You mind explaining a bit what's your intent for the views here? I believe views are used to pre-process a data and hence allow quick response through a simply select when user request the data or to hind column(s) for security purpose. Your use don't fall in these two purpose, so I got a bit curious. Muriithi
2) Use Below to Sum and Arrange from the View
select `viewalltables`.`Products` AS `Products`, sum((case when (`viewalltables`.`priority` = 'Immediate') then 1 else 0 end)) AS `A`, sum((case when (`viewalltables`.`priority` = 'Urgent') then 1 else 0 end)) AS `B`, sum((case when (`viewalltables`.`priority` = 'High') then 1 else 0 end)) AS `C`, sum((case when (`viewalltables`.`priority` = 'Normal') then 1 else 0 end)) AS `D`, sum((case when (`viewalltables`.`priority` = 'Low') then 1 else 0 end)) AS `E`, sum((case when (`viewalltables`.`name` = 'New') then 1 else 1 end)) AS `F`, sum((case when (`viewalltables`.`name` = 'Closed') then 1 else 0 end)) AS `G`, sum((case when (`viewalltables`.`name` = 'Resolved') then 1 else 0 end)) AS `H` from `viewalltables` group by `viewalltables`.`Products`;
3) i think it can cook well
On Thu, Aug 1, 2013 at 9:31 AM, Peter Karunyu <pkarunyu@gmail.com> wrote:
I have a table with 10 columns, and about 20,000 rows.
I want to get the count of the distinct values in 5 of the columns, so normally, I would probably write 5 queries thus:
SELECT column_a, count(column_a) FROM my_table GROUP BY column_a; SELECT column_b, count(column_b) FROM my_table GROUP BY column_b; SELECT column_c, count(column_c) FROM my_table GROUP BY column_c; SELECT column_d, count(column_d) FROM my_table GROUP BY column_d; SELECT column_e, count(column_e) FROM my_table GROUP BY column_e;
My question is, is there a way I can do this in a more generic/future proof way?
Maybe a stored procedure which I just pass the table name to? Or some PHP function which I just give the name of the table to?
Ultimate goal is to write just one query to get the above data.
______________________________________
participants (1)
-
William Muriithi