Solved

Find Median using SQL Server 2000

Posted on 2012-03-28
9
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

738 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