Click here to Skip to main content
16,012,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my table.
SQL
select * from table1

--------------------------------------------------------------------------------
name v1 v2 v3
--------------------------------------------------------------------------------
a 1.2 1.2 2.0
b 2.1 2.4 3.4
c 1.0 8.5 0.56
d 2.7 2.5 2.6
--------------------------------------------------------------------------------
I need this result.
--------------------------------------------------------------------------------
name v1 v2 v3
--------------------------------------------------------------------------------
avg 1.75 3.65 2.14(this new row)
median 1.65 2.45 2.3 (THIS new row)
a 1.2 1.2 2.0
b 2.1 2.4 3.4
c 1.0 8.5 0.56
d 2.7 2.5 2.6
--------------------------------------------------------------------------------
The row Avg is v1 values(1.2,2.1,1.0,2.7) is 1.75
v2 values (1.2,2.4,8.5,2.5) is 3.65
v3 values (2.0,3.4,0.56,2.6) is 2.14

The row avg is added dynamically using avg().

The row Median is half of the value. It is dynamically added
v1(1.2,2.1,1.0,2.7) median is 1.65.

Please anyone help me. Thanks a lot.
Posted
Updated 21-Dec-11 19:48pm
v4

Continue to Mika Wendelius answer..,
see below link also..
Here[^]
 
Share this answer
 
One example here: http://weblogs.sqlteam.com/jeffs/archive/2010/08/30/sql-server-calculate-median.aspx[^]

You can also use CTE which would be performing more well. An example: http://www.sqlmag.com/article/tsql3/calculating-the-median-gets-simpler-in-sql-server-2005[^]

And also if you want, you can create your own aggregate, an example: Custom Aggregates in SQL Server[^]
 
Share this answer
 
Comments
thatraja 22-Dec-11 1:59am    
5!
Wendelius 22-Dec-11 2:24am    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900