Jump to content


Photo

sums of many columns


  • Please log in to reply
4 replies to this topic

#1 guw

guw
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 May 2003 - 01:16 AM

I know the syntax for summing a column: SELECT * , sum( col_one ) AS \'1\' FROM some_table .
My question: is there a way to do the same for 288 columns at once, without having to issue 288 SELECT statements or creating one incredibly long SELECT statement like SELECT * , sum( col_one ) AS \'1\', sum( col_two ) AS \'2\', sum( col_3 ) AS \'3\', sum( col_4 ) AS \'4\', sum( col_5 ) AS \'5\' ... etc. ... FROM some_table? :?:

#2 pallevillesen

pallevillesen
  • Members
  • PipPipPip
  • Advanced Member
  • 135 posts
  • LocationDenmark

Posted 27 May 2003 - 07:09 AM

SELECT sum(col_one + col_two....+col_288) as total288 from some_table;

If you mean getting 288 sums, I would use php to fetch the coloumn names, then do 288 php generated searches - or simply use php to build your mysql SELECT statement...

And of pure curiousity, why on earth do you have a table design with 288 variables?

P.
Palle Villesen, www.birc.dk [br]Bioinformatics Research Center

#3 guw

guw
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 May 2003 - 09:42 PM

Yes, you guessed it right: I need to have 288 separate sums.

To satisfy your curiosity: the table(s) contain(s) values collected at 5 minute intervals during a 24 hour cycle. These daily data collections are used to generate a graph of cumulative values for display of various timeframes (last quarter, previous half year, Fridays only, all year except certain days, etc.) in the 24 hour format. For reliability each data point needs to be editable / reviewable. All this applies to 21 different data sets.

I had considered other designs, but found this the most space (and hopefully time-) conserving approach.

I had hoped to find something in the way of mysql_fetch_array or \"mysql_fetch_col\" (if that existed!) but had no success. So, I\'ll probably have to use a clunky SELECT statement with 288 SUM(..)s

#4 pallevillesen

pallevillesen
  • Members
  • PipPipPip
  • Advanced Member
  • 135 posts
  • LocationDenmark

Posted 28 May 2003 - 08:29 AM

Ok, but creating a big select statement using php is easy though...

And... you COULD have made a table with two coloumns:

TIME DATA (where TIME would have 288 different values...)

But I will give you that reability is a little better for 288 coloumns...

P.
Palle Villesen, www.birc.dk [br]Bioinformatics Research Center

#5 guw

guw
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 May 2003 - 09:33 AM

Thank you for at least participating in the \"philosophical\" part of the issue!

I wasn\'t too concerned about creating the SELECT statement, I was much more concerned about the poor server having to execute it! :) (That is also my reason for maintaining a \"day of week\" field - to prevent a recalculation each time a certain weekday is requested)
But then I visited a South African newspaper website (which must\'ve crashed right then, because it showed their error message) whose SQL statement contained about 500 values in their call! Now I don\'t feel so demanding anymore.

Your 2 column solution would require at least 3, because I would have to keep track of the date in addition to the time and the value - otherwise the filtering would not be possible. Plus the \"day of week\" field, a check field for verification purposes, and possibly some \"marker fields\".

Thanks again for your support!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users