PDA

View Full Version : Can this query be simplied?


many_tentacles
02-25-2010, 03:04 PM
I have the following query...

SELECT (SELECT COUNT(*) FROM my_table WHERE col1 = 'Y') AS Total1, (SELECT COUNT(*) FROM my_table WHERE col2 = 'Y') AS Total2, (SELECT COUNT(*) FROM my_table WHERE col3 = 'Y') AS Total3, (SELECT COU... etc etc

This will carry on until I get to col150. Do i really need to repeat the query that many times to create a results table?

thanks

guelphdad
02-25-2010, 03:31 PM
seems to me you'd need to normalize your data.

BubikolRamios
02-25-2010, 04:01 PM
That means:

1 create table detail (my_table beeing master)
2.


insert into detail select id, 'col1', col1 from my_table;
insert into detail select id, 'col2', col2 from my_table;
etc .



There you will have it, stuff for simpler select.