Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex summing in SQL

I'm working with a relational database that uses SQL99.

I have a series of 10 columns, each of the 10 columns contain a number value.

I need to sum each column individually and then take those sums and add them all together to get an overall sum. Then I must divide the overall sum by 15.

I've tried every format I can think of and have yet to return any results. I have no idea what the syntax should look like.

like image 520
user1255695 Avatar asked Mar 21 '26 20:03

user1255695


2 Answers

SELECT SUM(col1), SUM(col2)..., SUM(col1 + col2 + col3 + col4...)/15
FROM TABLENAME
GROUP BY 1=1
like image 162
Sam DeHaan Avatar answered Mar 24 '26 02:03

Sam DeHaan


select 
      sum(col1) as sum1, 
      sum(col2) as sum2, 
      sum(col3) as sum3, 
      sum(col4) as sum4,
      sum(col5) as sum5, 
      sum(col6) as sum6, 
      sum(col7) as sum7, 
      sum(col8) as sum8,
      sum(col9) as sum9, 
      sum(col10) as as sum10,
      sum( col1 + col2 + col3 + col4 + col5 + col6 + col7 + col8 + col9 + col10) as overallsum,
      sum( col1 + col2 + col3 + col4 + col5 + col6 + col7 + col8 + col9 + col10) / 15 as dividedsum
   from 
      tablename
like image 31
Lynn Crumbling Avatar answered Mar 24 '26 02:03

Lynn Crumbling