Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need to determine median in an existing query

Author  Topic 

jedinerd
Starting Member

4 Posts

Posted - 2010-07-22 : 19:07:50
[code]select datePart(dy,s.Time) as theday, DATEPART(YY,s.Time) as theyear,
AVG(cast(s.TotalCount as bigint)) as dayaverage,
MIN(cast(s.TotalCount as bigint)) as daymin,
MAX(cast(s.TotalCount as bigint)) as daymax
from SiteStats.OnlineNowHistory as s
where s.Time > '2010-6-19 00:00:00' and
s.Type = 1 --type 1 myspace type 2 instant messenger
and s.TotalCount >= 1
group by datePart(dy,s.Time), datePart(yy,s.Time)[/code]

That is my current working query which returns avg min and max per day on a dataset that is several years of stats in 5 minute intervals. I also would like to return the median as daymedian. Can someone help me out?

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-07-22 : 19:15:40
have a look here http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/9ff324f6-854c-4a39-9e8b-0ab9d3151558

both Adam Mechanic and Itzik Ben-Gan have solutions for Median
Go to Top of Page

jedinerd
Starting Member

4 Posts

Posted - 2010-07-22 : 19:22:49
I have read the first 100 or so responses in google relating to median and ms sql and have yet to be successful in integrating any of them into my current solution based on the examples provided. I had hoped someone could assist me in this endeavor as i have been toiling at it for days...
quote:
Originally posted by russell

have a look here http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/9ff324f6-854c-4a39-9e8b-0ab9d3151558

both Adam Mechanic and Itzik Ben-Gan have solutions for Median

Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-07-22 : 20:47:24
Create a function like this
Create Function SiteStats.Median (@day int, @year int)
Returns decimal (9, 3)
AS
BEGIN
Declare @Median decimal (9, 3)

Declare @h1 bigint
Declare @h2 bigint

SELECT @h1 = MAX(totalcount) FROM (
SELECT TOP 50 PERCENT totalcount
FROM SiteStats.OnlineNowHistory
WHERE DatePart(dy, [Time]) = @day
And DatePart(yy, [Time]) = @year
ORDER BY totalcount
) h1

SELECT @h2 = MIN(totalcount) FROM (
SELECT TOP 50 PERCENT totalcount
FROM SiteStats.OnlineNowHistory
WHERE DatePart(dy, [Time]) = @day
And DatePart(yy, [Time]) = @year
ORDER BY totalcount DESC
) h2

SELECT @Median = (@H1 + @H2) / 2.0

Return @Median
END


Call it in your query like this

select datePart(dy,s.Time) as theday, DATEPART(YY,s.Time) as theyear,
AVG(cast(s.TotalCount as bigint)) as dayaverage,
MIN(cast(s.TotalCount as bigint)) as daymin,
MAX(cast(s.TotalCount as bigint)) as daymax,
SiteStats.median(datePart(dy,s.Time), DATEPART(YY,s.Time)) as daymedian
from SiteStats.OnlineNowHistory as s
where s.Time > '2010-6-19 00:00:00'
and s.Type = 1 --type 1 myspace type 2 instant messenger
and s.TotalCount >= 1
group by
datePart(dy,s.Time), datePart(yy,s.Time)
Go to Top of Page
   

- Advertisement -