2012-03-07 23 views
2

私はSQL99を使用するリレーショナルデータベースを扱っています。SQLの複素数合計

私は10列の列を持ち、10列にはそれぞれ数値が入っています。

私は各列を個別に合計し、それらの合計を合計して合計して全体の合計を求める必要があります。それで私は総計を15で割る必要があります。

私は思っていて、結果を返すことはできません。私は構文がどのように見えるか分からない。

+0

あなただけの1行が返さたいですか? ___でグループ化された複数の行 –

+0

しかし、特にRDBMSとは何ですか? – bfavaretto

+0

申し訳ありませんが、Oracleシステムと私が見る必要があるのは総額の結果です/ 15 – user1255695

答えて

0
SELECT SUM(subsum)/15 FROM (
    SELECT SUM(column1) AS subsum 
     FROM table 
    UNION ALL 
    SELECT SUM(column2) AS subsum 
     FROM table 
    UNION ALL 
    ... 
    SELECT SUM(column10) AS subsum 
     FROM table 
) 
6
SELECT SUM(col1), SUM(col2)..., SUM(col1 + col2 + col3 + col4...)/15 
FROM TABLENAME 
GROUP BY 1=1 
+0

ニース...私のバージョンよりも優れています! –

+3

COALESC()またはIFNULL()のいくつかに入れてNULLsに注意してください – wildplasser

3
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