Q:

Vertical select results instead horizontal results SQL Server

Lets say I have a table like:
a     b    c     d    e      f
---------------------------------
1.2  2.3  4.4   5.1  6.7    11.9   
7.2  2.3  4.3   5.1  4.7    3.9   
1.9  5.3  3.3   5.1  3.7    8.9   
5.2  2.7  7.4   9.1  1.7    2.9  

If I have to compute something as the sqrt of the multiplication of the sum of the columns 
SQRT(sum(a*a)), SQRT(sum(a*b)), SQRT(sum(a*c)), SQRT(sum(a*d)), SQRT(sum(a*e)), 
SQRT(sum(a*f)), SQRT(sum(b*b)), SQRT(sum(b*c)), SQRT(sum(b*d)).... SQRT(sum(f*f))

Results:
id  result  operation
-----------------------------
1    x       a*a
2    y       a*b
...  ...     ...
n    z       f*f  

instead of:
a*a     a*b   ...     f*f 
----------------------------
 x       y    ...      z

currently the query is 
SELECT SQRT(sum(a*a)) AS a*a, SQRT(sum(a*b)) AS a*b, ...  , SQRT(sum(f*f)) AS f*f 
FROM Sometable

A:

DECLARE @T TABLE(
id INT PRIMARY KEY, 
a FLOAT,
b FLOAT,
c FLOAT,
d FLOAT,
e FLOAT, 
f FLOAT
)

INSERT INTO @T
SELECT * FROM (VALUES
(1, 1.2,2.3,4.4,5.1,6.7,11.9),
(2, 7.2,2.3,4.3,5.1,4.7,3.9), 
(3, 1.9,5.3,3.3,5.1,3.7,8.9), 
(4, 5.2,2.7,7.4,9.1,1.7,2.9) )  T(id,a,b,c,d,e, f)

;WITH U AS
(
SELECT * 
FROM @T
UNPIVOT (o FOR col IN (a,b,c,d,e, f)) unpvt
)
SELECT u1.col + '*' + u2.col as operation, SQRT(sum(u1.o*u2.o)) AS result
FROM U u1 JOIN U u2 ON u1.id=u2.id  AND u1.col <= u2.col
GROUP BY u2.col, u1.col
ORDER BY u2.col, u1.col

