Custom Search
www.rocket99.com : Technical Guides Sybase Oracle UNIX Javascript


Technical Guides
Sybase
Oracle
UNIX
Javascript




Of Interest

Business Intelligence and Analytics



Oracle Training





Sybase » Transact-SQL » Coding » Median

Median Calculation, with pure SQL

     



What is a median?



A median is the midpoint in a series of numbers; half of the values are greater than the median, half are less.







Manually calculating a median involves:




- Ranking the data

- Determine the positional midpoint (roughly the number of values divided by 2)

- Select the value ranked closest to the positional midpoint, high side and low side

- Average the two resulting values







Examples

21
23
35 <---
63
75

Median is 35 (easy when there is odd # of items)



21
23
32 ______
35
63
75

Median is 33.5




create table #rank (
rank_id numeric(9) identity,
region_id integer not null,
gross_sales money null
)
go

--set rowcount 11
set rowcount 28
go

insert into #rank (region_id, gross_sales)
select region_id, gross_sales
from sales_data
where fiscal_month_id = 200609 and gross_sales is not null
order by gross_sales desc
go

set rowcount 0
go



select * from #rank order by rank_id
go



declare @gross_sales1 integer, @gross_sales2 integer, @midpoint float, @count
integer

select @count = count(1) from #rank
select @midpoint = floor (@count / 2)


-- Get the half-way point, sort of.

if (@count % 2) > 0
select @midpoint = @midpoint + 1
else
select @midpoint = @midpoint + 0.000001

select 'midpoint: ' , @midpoint


-- Get value closest to midpoint, low side

select @gross_sales1 = gross_sales from #rank where rank_id <= @midpoint
order by rank_id


-- Get value closest to midpoint, high side

select @gross_sales2 = gross_sales from #rank where rank_id >= @midpoint
order by rank_id desc

select @gross_sales1, @gross_sales2


-- Return median

select (@gross_sales1 + @gross_sales2)/2.0 'MEDIAN'
go

select avg(gross_sales) 'AVERAGE' from #rank
go


drop table #rank
go











Sybase : Related Topics

Sybase : Transact-SQL : Transact SQL: numeric functions
Sybase : Transact-SQL : Transact SQL: string functions
Sybase : Transact-SQL : Transact SQL: date/time functions
Sybase : Transact-SQL : Transact SQL: misc functions
Sybase : Transact-SQL : Transact SQL: Conditionals
Sybase : Transact-SQL : Transact SQL: looping constructs
Sybase : Transact-SQL : Transact SQL: Cursors
Sybase : Transact-SQL : Transact SQL: Complex Updates
Sybase : Transact-SQL : Transact SQL: Finding duplicate rows in a table
Sybase : Transact-SQL : Using Temporary Tables
Sybase : Transact-SQL : Inner/Outer Joins
Sybase : Transact-SQL : Isolation Levels
Sybase : Transact-SQL : Reporting: SQL Performance and Tuning
Sybase : Transact-SQL : Case Statement
Sybase : Transact-SQL : Date processing: stripping time from datetime
Sybase : Transact-SQL : Safely delete a large number of rows without blowing the t-log
Sybase : Transact-SQL : Transact SQL: date/time formats
Sybase : Transact-SQL : Creating a stored procedure
Sybase : Transact-SQL : Custom query plans
Sybase : Transact-SQL : Rowcount setting - limiting result sets
Sybase : Transact-SQL : Ranking Data - with Duplicates
Sybase : Transact-SQL : Forcing an index in a query
Sybase : Transact-SQL : Lead and Lag with pure SQL

Sybase Web Site
Sybase iAnywhere Mobile Web Site
Oracle Enterprise Web Site



Get the latest Rocket99 news and tech tips via






Site Index About this Guide to Sybase, Oracle, and UNIX Contact Us Advertise on this site




Copyright © 2016 Stoltenbar Inc All Rights Reserved.