--This is how you can compute some numbers that show some central

--value concepts. Run the entire script and don't forget to take a

--look at the messages tab for the histogram.

--By C. Eric Cashon

USE tempdb

 

CREATE TABLE SampleValues(

    List real)

 

DECLARE @i int

DECLARE @j int

SET NOCOUNT ON

SET @i = 0

WHILE @i <= 100

  BEGIN

    INSERT INTO SampleValues VALUES (@i)

    SET @i = @i + 1

  END

/*

--Check your table.

SELECT * FROM SampleValues

--Get all combinations of two

SELECT T1.List, T2.List, ((T1.List + T2.List)/2) AS HalfTotal

         FROM SampleValues T1 CROSS JOIN SampleValues T2

         WHERE T1.List < T2.List

         ORDER BY HalfTotal

*/

--Load the sample mean values into a new table of all the combinations

--of two.

SELECT T3.HalfTotal INTO Combo2 FROM

       (SELECT ((T1.List + T2.List)/2) AS HalfTotal

         FROM SampleValues T1 CROSS JOIN SampleValues T2

         WHERE T1.List < T2.List) AS T3

        

--Make a symetrical histogram

DECLARE @x int

DECLARE @y int

DECLARE @HalfTotal decimal(4,1)

DECLARE @String varchar(500)

SET @String = ''

SET @y = 1

DECLARE Histogram CURSOR FAST_FORWARD FOR

SELECT COUNT(HalfTotal) AS Sets, HalfTotal

  FROM Combo2

  GROUP BY HalfTotal

  ORDER BY HalfTotal

OPEN Histogram

FETCH NEXT FROM Histogram INTO @x, @HalfTotal

WHILE @@FETCH_STATUS = 0

BEGIN

 WHILE @y <= @x

    BEGIN

       SET @String = @String + '*'

       SET @y = @y + 1

    END

    SET @String = CAST(@HalfTotal as varchar(10)) + @String

   PRINT @String

  SET @String = ''

  SET @y = 1

 FETCH NEXT FROM Histogram INTO @x, @HalfTotal

END

CLOSE Histogram

DEALLOCATE Histogram

 

--Clean up

DROP TABLE SampleValues

DROP TABLE Combo2

---------------------------------------------------------------------------------

/*Estimate the central value by taking 30 sets of a small sample size and

  30 sets of a larger sample size. Also calculate the standard deviation

  of the sampling distribution.

*/

CREATE TABLE AverageFive(

      Five real)

CREATE TABLE AverageThirty(

      Thirty real)

CREATE TABLE #Temp(

      Value int)

 

    DECLARE @RandNumber int

    DECLARE @Count1 int

    DECLARE @Count2 int

 

    SET @Count1 = 1

    SET @Count2 = 1

      WHILE @Count1 <= 30

       BEGIN

        WHILE @Count2 <= 5

          BEGIN

            SET @RandNumber = RAND() * 100

            INSERT INTO #Temp VALUES (@RandNumber)

            SET @Count2 = @Count2 + 1

          END

         INSERT INTO AverageFive (Five) SELECT AVG(Value) FROM #Temp

         TRUNCATE TABLE #Temp

         SET @Count2 = 1

         SET @Count1 = @Count1 + 1

       END

 

    SET @Count1 = 1

    SET @Count2 = 1

      WHILE @Count1 <= 30

       BEGIN

        WHILE @Count2 <= 30

          BEGIN

            SET @RandNumber = RAND() * 100

            INSERT INTO #Temp VALUES (@RandNumber)

            SET @Count2 = @Count2 + 1

          END

         INSERT INTO AverageThirty (Thirty) SELECT AVG(Value) FROM #Temp

         TRUNCATE TABLE #Temp

         SET @Count2 = 1

         SET @Count1 = @Count1 + 1

       END

 

SELECT AVG(Five) CentralValue, STDEV(Five) StandardDeviation FROM AverageFive

SELECT AVG(Thirty) CentralValue, STDEV(Thirty) StandardDeviation FROM AverageThirty

 

--Clean up

DROP TABLE AverageFive

DROP TABLE AverageThirty

DROP TABLE #Temp