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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ
 All Forums  Site Related Forums  Article Discussion  Article: Statistics in SQL (Sample Chapter) Reply to Topic  Printer Friendly
Author  Topic

USA
0 Posts

 Posted - 05/27/2002 :  11:40:15 Ales Spetic submitted "While SQL was never designed to be a statistics package, the language is quite versatile and holds a lot of hidden potential when it comes to calculating statistics. If you need a fast, easy-to-use tool for quick analysis of an existing data set, then the concepts presented in this article may prove quite helpful. The text covers topics like: Means, Modes, Medians Standard deviations, Variances, Standard errors. Confidence intervals, Correlations, Moving averages, Weighted moving averages."Article Link.

jsmith8858
Dr. Cross Join

USA
7423 Posts

 Posted - 11/08/2002 :  15:42:25 Here's any easier way to get a median (not the financial median, however):SELECT x.Value AS medianFROM Vals AS x, Vals AS yGROUP BY x.ValueHAVING SUM(SIGN(x.Value-y.Value)) IN (1,0)Change the " IN (1,0)" to a -1 to get the lower value if there is an even # of values. Currently, the higher # is returned.Basically, we are saying compare each number to all possible numbers, and get a adds values of 1,0 or -1 if the first number is less, equal or higher than the second. The number that returns 0 is right in the middle ... If there is no middle, a -1 or 1 is returned.To get the financial median, you need to encapsulate the results of the above into a subquery, allow for not just (-1,0) but all three (-1,0,1) and then take the AVG of the values returned.That is,SELECT Avg(Median) as Median FROM( SELECT x.Value AS median FROM Vals AS x, Vals AS y GROUP BY x.Value HAVING SUM(SIGN(x.Value-y.Value)) IN (1,0,-1))That should do the trick. Let me know what y'all think.

kbiel
Starting Member

2 Posts

 Posted - 07/01/2005 :  16:33:22 quote:Originally posted by jsmith8858Here's any easier way to get a median (not the financial median, however):SELECT x.Value AS medianFROM Vals AS x, Vals AS yGROUP BY x.ValueHAVING SUM(SIGN(x.Value-y.Value)) IN (1,0)That should do the trick. Let me know what y'all think.It's a neat trick, but it does not work for non-unique values approaching the median.

kbiel
Starting Member

2 Posts

 Posted - 07/01/2005 :  17:52:38 quote:Originally posted by kbielquote:Originally posted by jsmith8858That should do the trick. Let me know what y'all think.It's a neat trick, but it does not work for non-unique values approaching the median.This should work:``` DECLARE @Vals TABLE ( Value INT, i INT identity(1,1) ) INSERT INTO @Vals SELECT SomeValueColumn FROM SomeTable ORDER BY SomeValueColumn ASC SELECT AVG(Value) FROM @Vals WHERE i = (SELECT (MAX(i) + 1) / 2 + (MAX(i) + 1) % 2 FROM @Vals) OR i = (SELECT (MAX(i) + 1) / 2 FROM @Vals) ```That will select the financial median. To select only the middle upper value on an even number of records, then use the following WHERE clause:``` WHERE i = (SELECT (MAX(i) + 1) / 2 + (MAX(i) + 1) % 2 FROM @Vals) ```To select only the middle lower value on an even number of records then use this WHERE clause:``` WHERE i = (SELECT MAX(i) / 2 + MAX(i) % 2 FROM @Vals) ```
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC