Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to find the median value in a row in sql server. but i couldn't get it any answer. below query i'm tried.


select 
(select cast(Avg(TotAvg * 1.0 )as decimal(6,2)) from (values (convert(decimal(6,2), a)),(convert(decimal(6,2), b)),(convert(decimal(6,2), c)),
(convert(decimal(6,2), d)),(convert(decimal(6,2), e)),
(convert(decimal(6,2), f)),(convert(decimal(6,2), g)),(convert(decimal(6,2), h)),(convert(decimal(6,2), i)),
(convert(decimal(6,2), j)),(convert(decimal(6,2), k)),(convert(decimal(6,2), l)),(convert(decimal(6,2), m)),
(convert(decimal(6,2), n)),(convert(decimal(6,2), o)),(convert(decimal(6,2), p)),(convert(decimal(6,2), q)),
(convert(decimal(6,2), r)),(convert(decimal(6,2), s)),(convert(decimal(6,2), s)),(convert(decimal(6,2), u))) as Totalavg(TotAvg)) 
as Median

from temp


What I have tried:

select 
(select cast(Avg(TotAvg * 1.0 )as decimal(6,2)) from (values (convert(decimal(6,2), a)),(convert(decimal(6,2), b)),(convert(decimal(6,2), c)),
(convert(decimal(6,2), d)),(convert(decimal(6,2), e)),
(convert(decimal(6,2), f)),(convert(decimal(6,2), g)),(convert(decimal(6,2), h)),(convert(decimal(6,2), i)),
(convert(decimal(6,2), j)),(convert(decimal(6,2), k)),(convert(decimal(6,2), l)),(convert(decimal(6,2), m)),
(convert(decimal(6,2), n)),(convert(decimal(6,2), o)),(convert(decimal(6,2), p)),(convert(decimal(6,2), q)),
(convert(decimal(6,2), r)),(convert(decimal(6,2), s)),(convert(decimal(6,2), s)),(convert(decimal(6,2), u))) as Totalavg(TotAvg)) 
as Median

from temp
Posted
Updated 3-May-18 20:20pm

Perhaps if you didn't have two s values in there:
(convert(decimal(6,2), r)),(convert(decimal(6,2), s)),(convert(decimal(6,2), s)),
                                                  ^                          ^
It might help?
 
Share this answer
 
Comments
IsaiSelvan 4-May-18 5:29am    
Table Values
============
First row - 1,5,6,7,8,2,6,3,4,5,2,1,6,5,7,8,2,7,6,2,8
Second row - 1,5,6,7,8,2,2,3,2,4,2,1,4,5,9,8,2,7,6,2,8
OriginalGriff 4-May-18 5:34am    
What do you get when you run the query?
What did you expect to get?
What does the data input look like?
IsaiSelvan 4-May-18 5:37am    
Am already tried that format. the below is the sample data with query.

select
(select cast(Avg(TotAvg * 1.0 )as decimal(6,2)) from (values (convert(decimal(6,2), a)),(convert(decimal(6,2), b)),(convert(decimal(6,2), c)),
(convert(decimal(6,2), d)),(convert(decimal(6,2), e)),
(convert(decimal(6,2), f)),(convert(decimal(6,2), g)),(convert(decimal(6,2), h)),(convert(decimal(6,2), i)),
(convert(decimal(6,2), j)),(convert(decimal(6,2), k)),(convert(decimal(6,2), l)),(convert(decimal(6,2), m)),
(convert(decimal(6,2), n)),(convert(decimal(6,2), o)),(convert(decimal(6,2), p)),(convert(decimal(6,2), q)),
(convert(decimal(6,2), r)),(convert(decimal(6,2), s)),(convert(decimal(6,2), t)),(convert(decimal(6,2), u))) as Totalavg(TotAvg))
as Median from temp

I changed you mentioned column. help me


column values are a to u in a row.

Table Values
============
First row - 1,5,6,7,8,2,6,3,4,5,2,1,6,5,7,8,2,7,6,2,8
Second row - 1,5,6,7,8,2,2,3,2,4,2,1,4,5,9,8,2,7,6,2,8
OriginalGriff 4-May-18 5:48am    
And? What happens when you run it, and what did you expect to happen?
IsaiSelvan 4-May-18 5:54am    
Yes i want to get median for each row.

First Row - 1,5,6,7,8,2,6,3,4,5,2,1,6,5,7,8,2,7,6,2,8

Actuval Value - 4.80 (this is wrong)
Expected value - 5 (Correct Output)

Second Row- 1,5,6,7,8,2,2,3,2,4,2,1,4,5,9,8,2,7,6,2,8

Actual Value - 4.47 (this is wrong)
Expected Value - 4 (Correct Output)
Not an answer, but additional information...

What is the fastest way to calculate the median? - SQLPerformance.com[^]
 
Share this answer
 
Comments
IsaiSelvan 4-May-18 5:37am    
Am already tried that format. the below is the sample data with query.

select
(select cast(Avg(TotAvg * 1.0 )as decimal(6,2)) from (values (convert(decimal(6,2), a)),(convert(decimal(6,2), b)),(convert(decimal(6,2), c)),
(convert(decimal(6,2), d)),(convert(decimal(6,2), e)),
(convert(decimal(6,2), f)),(convert(decimal(6,2), g)),(convert(decimal(6,2), h)),(convert(decimal(6,2), i)),
(convert(decimal(6,2), j)),(convert(decimal(6,2), k)),(convert(decimal(6,2), l)),(convert(decimal(6,2), m)),
(convert(decimal(6,2), n)),(convert(decimal(6,2), o)),(convert(decimal(6,2), p)),(convert(decimal(6,2), q)),
(convert(decimal(6,2), r)),(convert(decimal(6,2), s)),(convert(decimal(6,2), t)),(convert(decimal(6,2), u))) as Totalavg(TotAvg))
as Median from temp

I changed you mentioned column. help me


column values are a to u in a row.

Table Values
============
First row - 1,5,6,7,8,2,6,3,4,5,2,1,6,5,7,8,2,7,6,2,8
Second row - 1,5,6,7,8,2,2,3,2,4,2,1,4,5,9,8,2,7,6,2,8
Maciej Los 4-May-18 5:47am    
And you want to get median for each row?
What's your issue?
IsaiSelvan 4-May-18 5:56am    
Yes i want to get median for each row.

First Row - 1,5,6,7,8,2,6,3,4,5,2,1,6,5,7,8,2,7,6,2,8

Actuval Value - 4.80 (this is wrong)
Expected value - 5 (Correct Output)

Second Row- 1,5,6,7,8,2,2,3,2,4,2,1,4,5,9,8,2,7,6,2,8

Actual Value - 4.47 (this is wrong)
Expected Value - 4 (Correct Output)


Create table Script
===================
create table temp (id int identity(1,1), a decimal(6,2),b decimal(6,2),c decimal(6,2),d decimal(6,2),e decimal(6,2),f decimal(6,2),g decimal(6,2),h decimal(6,2),i decimal(6,2),j decimal(6,2),k decimal(6,2),l decimal(6,2),m decimal(6,2),n decimal(6,2),o decimal(6,2),p decimal(6,2),q decimal(6,2),r decimal(6,2),
s decimal(6,2),t decimal(6,2),u decimal(6,2))

Insert Table Script:
====================
insert into temp (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u) values (1,5,6,7,8,2,6,3,4,5,2,1,6,5,7,8,2,7,6,2,8)
insert into temp (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u) values (1,5,6,7,8,2,2,3,2,4,2,1,4,5,9,8,2,7,6,2,8)

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