Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find Median using SQL Server 2000

Posted on 2012-03-28
9
Medium Priority
?
360 Views
Last Modified: 2012-04-11
I have Max() working as in following query and want to find Mean & Median.....

select 'Max' as Func, Max(b.Proc_Cnt) Procedures
from (
select a.Stu_Code, sum(a.proc_cnt) Proc_Cnt
from (
select s.stu_code, t.chart_no, t.last_date, count(*) proc_cnt
from Treatment  t, Student s, Patient p
where s.stu_code = t.provider_id_credited
and p.chart_no = t.chart_no
and t.last_date between '6/30/2009' and '7/1/2010'
and s.clinic_location like 'GRP%'
 and t.njds_code <> '00000'
 and (t.njds_code like 'D%' or t.njds_code like '0%')
and t.njds_code NOT BETWEEN '90000' and '90100'
and (t.chart_no Not Like 'M%' and t.chart_no Not Like 'F%' and t.chart_no <> '292021')
and (t.Activity Not in ('CP','CS','NS') OR t.Activity is Null)
and ((p.group_assignment IS NULL) OR
     (p.group_assignment <> 'MTHN' AND p.group_assignment <> 'NRTF'
  AND p.group_assignment <> 'SOM' AND p.group_assignment <> 'GWY'))
group by s.stu_code, t.chart_no, t.last_date) a
group by a.stu_code
) b

How do I convert above to get Median?

Thanks,
Ashok
0
Comment
Question by:Ashok
  • 5
  • 4
9 Comments
 
LVL 9

Expert Comment

by:rajeevnandanmishra
ID: 37778959
Hi,
MEAN can be calculated by using AVG function like:
select 'Max' as Func, Max(b.Proc_Cnt) Procedures
from (
select a.Stu_Code, sum(a.proc_cnt) Proc_Cnt
from (
select s.stu_code, t.chart_no, t.last_date, count(*) proc_cnt
from Treatment  t, Student s, Patient p
where s.stu_code = t.provider_id_credited
and p.chart_no = t.chart_no
and t.last_date between '6/30/2009' and '7/1/2010'
and s.clinic_location like 'GRP%'
 and t.njds_code <> '00000'
 and (t.njds_code like 'D%' or t.njds_code like '0%')
and t.njds_code NOT BETWEEN '90000' and '90100'
and (t.chart_no Not Like 'M%' and t.chart_no Not Like 'F%' and t.chart_no <> '292021')
and (t.Activity Not in ('CP','CS','NS') OR t.Activity is Null)
and ((p.group_assignment IS NULL) OR
     (p.group_assignment <> 'MTHN' AND p.group_assignment <> 'NRTF'
  AND p.group_assignment <> 'SOM' AND p.group_assignment <> 'GWY'))
group by s.stu_code, t.chart_no, t.last_date) a
group by a.stu_code
) b
0
 
LVL 9

Accepted Solution

by:
rajeevnandanmishra earned 2000 total points
ID: 37779005
And Median can be found by using below query:
select 'Median' as Func,
(SELECT top 1 Proc_Cnt FROM
(select top 50 percent Proc_Cnt
from (
select a.Stu_Code, sum(a.proc_cnt) Proc_Cnt
from (
select s.stu_code, t.chart_no, t.last_date, count(*) proc_cnt
from Treatment  t, Student s, Patient p
where s.stu_code = t.provider_id_credited
and p.chart_no = t.chart_no
and t.last_date between '6/30/2009' and '7/1/2010'
and s.clinic_location like 'GRP%'
 and t.njds_code <> '00000'
 and (t.njds_code like 'D%' or t.njds_code like '0%')
and t.njds_code NOT BETWEEN '90000' and '90100'
and (t.chart_no Not Like 'M%' and t.chart_no Not Like 'F%' and t.chart_no <> '292021')
and (t.Activity Not in ('CP','CS','NS') OR t.Activity is Null)
and ((p.group_assignment IS NULL) OR
     (p.group_assignment <> 'MTHN' AND p.group_assignment <> 'NRTF'
  AND p.group_assignment <> 'SOM' AND p.group_assignment <> 'GWY'))
group by s.stu_code, t.chart_no, t.last_date) a
group by a.stu_code
) b ORDER BY Proc_Cnt ) c ORDER BY 1 DESC )
+
(SELECT top 1 Proc_Cnt FROM
(select top 50 percent Proc_Cnt
from (
select a.Stu_Code, sum(a.proc_cnt) Proc_Cnt
from (
select s.stu_code, t.chart_no, t.last_date, count(*) proc_cnt
from Treatment  t, Student s, Patient p
where s.stu_code = t.provider_id_credited
and p.chart_no = t.chart_no
and t.last_date between '6/30/2009' and '7/1/2010'
and s.clinic_location like 'GRP%'
 and t.njds_code <> '00000'
 and (t.njds_code like 'D%' or t.njds_code like '0%')
and t.njds_code NOT BETWEEN '90000' and '90100'
and (t.chart_no Not Like 'M%' and t.chart_no Not Like 'F%' and t.chart_no <> '292021')
and (t.Activity Not in ('CP','CS','NS') OR t.Activity is Null)
and ((p.group_assignment IS NULL) OR
     (p.group_assignment <> 'MTHN' AND p.group_assignment <> 'NRTF'
  AND p.group_assignment <> 'SOM' AND p.group_assignment <> 'GWY'))
group by s.stu_code, t.chart_no, t.last_date) a
group by a.stu_code
) b ORDER BY Proc_Cnt DESC) c ORDER BY 1 )/2


Though it could be better if you put your inner processed result in a temp table and then run the query on that temp table.
0
 
LVL 13

Author Comment

by:Ashok
ID: 37779422
MEAN can be calculated by using AVG function like:

I do not see the use of AVG function in above.

I will try the median when I go to work tomorrow morning.

Thanks,
Ashok
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Author Comment

by:Ashok
ID: 37779426
Do I just change SUM to

select 'Avg' as Func, Avg(b.Proc_Cnt) Procedures

in my Max() query?

Ashok
0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
ID: 37779446
My mistake. Yes,just change the MAX to AVG in your query and that will provide you the MEAN.
0
 
LVL 13

Author Closing Comment

by:Ashok
ID: 37781931
Thanks a lot
0
 
LVL 13

Author Comment

by:Ashok
ID: 37782653
by the way, this did not work for my data.
Median was out of range (not between Min & Max).

Ashok
0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
ID: 37783561
Hi,
Logically it should work. But could be some mistake by me. Can you put the result of below two queries:
SELECT top 1 Proc_Cnt FROM 
(select top 50 percent Proc_Cnt 
from (
select a.Stu_Code, sum(a.proc_cnt) Proc_Cnt
from (
select s.stu_code, t.chart_no, t.last_date, count(*) proc_cnt
from Treatment  t, Student s, Patient p
where s.stu_code = t.provider_id_credited
and p.chart_no = t.chart_no 
and t.last_date between '6/30/2009' and '7/1/2010'
and s.clinic_location like 'GRP%'
 and t.njds_code <> '00000'
 and (t.njds_code like 'D%' or t.njds_code like '0%')
and t.njds_code NOT BETWEEN '90000' and '90100'
and (t.chart_no Not Like 'M%' and t.chart_no Not Like 'F%' and t.chart_no <> '292021')
and (t.Activity Not in ('CP','CS','NS') OR t.Activity is Null)
and ((p.group_assignment IS NULL) OR 
     (p.group_assignment <> 'MTHN' AND p.group_assignment <> 'NRTF' 
  AND p.group_assignment <> 'SOM' AND p.group_assignment <> 'GWY'))
group by s.stu_code, t.chart_no, t.last_date) a
group by a.stu_code
) b ORDER BY Proc_Cnt ) c ORDER BY 1 DESC 

SELECT top 1 Proc_Cnt FROM 
(select top 50 percent Proc_Cnt 
from (
select a.Stu_Code, sum(a.proc_cnt) Proc_Cnt
from (
select s.stu_code, t.chart_no, t.last_date, count(*) proc_cnt
from Treatment  t, Student s, Patient p
where s.stu_code = t.provider_id_credited
and p.chart_no = t.chart_no 
and t.last_date between '6/30/2009' and '7/1/2010'
and s.clinic_location like 'GRP%'
 and t.njds_code <> '00000'
 and (t.njds_code like 'D%' or t.njds_code like '0%')
and t.njds_code NOT BETWEEN '90000' and '90100'
and (t.chart_no Not Like 'M%' and t.chart_no Not Like 'F%' and t.chart_no <> '292021')
and (t.Activity Not in ('CP','CS','NS') OR t.Activity is Null)
and ((p.group_assignment IS NULL) OR 
     (p.group_assignment <> 'MTHN' AND p.group_assignment <> 'NRTF' 
  AND p.group_assignment <> 'SOM' AND p.group_assignment <> 'GWY'))
group by s.stu_code, t.chart_no, t.last_date) a
group by a.stu_code
) b ORDER BY Proc_Cnt DESC) c ORDER BY 1

Open in new window

0
 
LVL 13

Author Comment

by:Ashok
ID: 37833883
Proc_Cnt
321

(1 row(s) affected)

Proc_Cnt
322

(1 row(s) affected)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question