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
 Mean, Median, Mode

Author  Topic 

daipayan
Posting Yak Master

181 Posts

Posted - 2009-12-03 : 07:35:08
Hello there,

I want to calculate mean, median, mode, range & standard deviation of the following table:
CREATE TABLE [calculate] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[year] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[q8] [float] NOT NULL ,
[q9] [float] NOT NULL ,
CONSTRAINT [PK_calculate] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
) ON [PRIMARY]
GO

Data in the columns are:
INSERT INTO [Test].[dbo].[calculate]([year], [q8], [q9])
VALUES('First', 2, 3);
INSERT INTO [Test].[dbo].[calculate]([year], [q8], [q9])
VALUES('First', 4, 2);
INSERT INTO [Test].[dbo].[calculate]([year], [q8], [q9])
VALUES('Second', 2, 6);
INSERT INTO [Test].[dbo].[calculate]([year], [q8], [q9])
VALUES('Third', 1, 4);
INSERT INTO [Test].[dbo].[calculate]([year], [q8], [q9])
VALUES('Third', 2, 3);
INSERT INTO [Test].[dbo].[calculate]([year], [q8], [q9])
VALUES('Third',9, 1);

Now Standard Deviation & Mean is:
SSELECT [year], STDEV(q8), STDEV(q9) FROM [Test].[dbo].[calculate] group by year

SELECT [year], avg(q8), avg(q9) FROM [Test].[dbo].[calculate] group by year


But HOW TO CALCULATE MEDIAN & MODE & RANGE??

Daipayan

kbhere
Yak Posting Veteran

58 Posts

Posted - 2009-12-03 : 07:44:56
There are examples in the below mentioned link..
This has the TSQL code and explaination for MEDIAN & MODE & RANGE..
Kindly go through it.. This will help you a lot..

http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/calculating-mean-median-and-mode-with-sq


Balaji.K
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-12-03 : 07:48:47
Hi

Try this for Median


SELECT AVG([q8]) FROM
(
SELECT [q8] FROM (
SELECT TOP 1 [q8] = [q8] * 1.0 FROM
(
SELECT TOP 50 PERCENT [q8]
FROM [CALCULATE] ORDER BY [q8]
) tab_a
ORDER BY 1 DESC
) tab_1
UNION ALL
SELECT [q8] FROM (
SELECT TOP 1 [q8] = [q8] * 1.0 FROM
(
SELECT TOP 50 PERCENT [q8]
FROM [CALCULATE] ORDER BY [q8] DESC
) tab_b
ORDER BY 1
) tab_2
) median


-------------------------
R...
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-12-03 : 08:13:04
THANKS A LOT to both of you!

Daipayan
Go to Top of Page

kbhere
Yak Posting Veteran

58 Posts

Posted - 2009-12-03 : 08:17:53
quote:
Originally posted by daipayan

THANKS A LOT to both of you!

Daipayan



No problem.. You are always WELCOME..


Balaji.K
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-12-14 : 23:47:57
Sir,

How to use this following Median Calculation T-SQL group wise?
Means I want the median Year wise!

Daipayan
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-12-15 : 02:20:18
There are more efficient ways to calculate the median.
Here are two ways
http://weblogs.sqlteam.com/peterl/archive/2009/09/16/Median-and-weighted-median.aspx



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-12-15 : 03:43:51
Sir,
Is the following link for MS SQL 2005 cause am using MS SQL 2000.

quote:
Originally posted by Peso

There are more efficient ways to calculate the median.
Here are two ways
http://weblogs.sqlteam.com/peterl/archive/2009/09/16/Median-and-weighted-median.aspx



N 56°04'39.26"
E 12°55'05.63"




Daipayan
Go to Top of Page
   

- Advertisement -