Solved

Find Median using SQL Server 2000

Posted on 2012-03-28
9
356 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

632 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