Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nulls in median calculation 1

Status
Not open for further replies.

lcfc

Programmer
Dec 4, 2001
27
GB
I am using this code to obtain the median from a field:

SELECT Min([Time Arrive to Dr]) FROM [All]
WHERE [Time Arrive to Dr] IN
(SELECT TOP 50 PERCENT [Time Arrive to Dr] FROM [All] ORDER BY [Time Arrive to Dr] DESC)
UNION SELECT Max([Time Arrive to Dr]) FROM [All]
WHERE [Time Arrive to Dr] IN (SELECT TOP 50 PERCENT [Time Arrive to Dr] FROM [All] ORDER BY [Time Arrive to Dr] ASC);

This produces either 1 number or 2, from which i can find the average and thus get the median.

Unfortunatley there are some null cells in the field which i don't want to include in the calculation. How can i filter these out, i.e, where do i put "is not null" in the above code. Thanks

 
In each of the top 50 percent subqueries

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Could you show me, in this query, I have tried but cannot seem to work it out?
 
Unfortunatley there are some null cells in the field
What is(are) the column(s) that may be null ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
In this field - [Time Arrive to Dr]

Thanks
 
Have you tried this ?[tt]
SELECT Min([Time Arrive to Dr])
FROM [All]
WHERE [Time Arrive to Dr] IN
(SELECT TOP 50 PERCENT [Time Arrive to Dr] FROM [All]
WHERE [Time Arrive to Dr] IS NOT NULL
ORDER BY [Time Arrive to Dr] DESC)
UNION
SELECT Max([Time Arrive to Dr])
FROM [All]
WHERE [Time Arrive to Dr] IN
(SELECT TOP 50 PERCENT [Time Arrive to Dr] FROM [All]
WHERE [Time Arrive to Dr] IS NOT NULL
ORDER BY [Time Arrive to Dr] ASC);[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hey that worked a treat, many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top