December 3, 2012 at 9:33 am
hi friend i have a small doubt in sql plz tell me how to solve
How to find (calculate) median using Sql Query...................
table data condian like
empid, deptid, salary
1, 100, 5000
2, 100, 3000
3, 100, 4000
5, 200, 6000
6, 200, 8000
i want output like
empid, deptid, salary, median
1, 100, 5000, 5000
2, 100, 3000, 5000
3, 100, 4000, 5000
5, 200, 6000, 5000
6, 200, 8000, 5000
and another ouptput like
empid, deptid, salary, median
1, 100, 5000, 4000
2, 100, 3000, 4000
3, 100, 4000, 4000
5, 200, 6000, 7000
6, 200, 8000, 7000
how to we implement the query in sql server plz tell me how to solve this.
December 3, 2012 at 11:06 am
Hi,
Try:
select
empid,
deptid,
salary,
AVG(salary) OVER() as median
from MyTable
and
select
empid,
deptid,
salary,
AVG(salary) OVER(PARTITION BY deptid) as median
from MyTable
Hope this helps.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply