Solved

Find Median using SQL Server 2000

Posted on 2012-03-28
9
344 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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
 
LVL 13

Author Comment

by:Ashok
Comment Utility
Do I just change SUM to

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

in my Max() query?

Ashok
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 9

Expert Comment

by:rajeevnandanmishra
Comment Utility
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
Comment Utility
Thanks a lot
0
 
LVL 13

Author Comment

by:Ashok
Comment Utility
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
Comment Utility
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
Comment Utility
Proc_Cnt
321

(1 row(s) affected)

Proc_Cnt
322

(1 row(s) affected)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 the fundamental information of how to create a table.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now