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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 T-test

Author  Topic 

jimris23
Starting Member

2 Posts

Posted - 2014-10-24 : 11:49:27
Hi everyone, I am trying to make a udf in T-sql which will take in to table columns and do a two sample t test. So in essence, what I want is a function which takes in two table columns, then takes the average, and variance of each column. Then computes a t value. To calculate the t-value the formula is t-value= (mean_col1-mean_col2)/sqrt(var_col1/count_col1+var_col2/count_col2).
Thanks
For your help in advance.
Jimmy Mirchandani

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-10-24 : 12:45:32
I wouldn't recommend using a udf for this. Do it in-line, or use a stored procedure, for exmaple like this:
;WITH cte AS
(
SELECT
AVG(col1) mean_col1, VAR(DISTINCT col1) var_col1,
COUNT(col1) AS count_col1,
AVG(col2) mean_col2, VAR(DISTINCT col2) var_col2,
COUNT(col2) AS count_col2
FROM
YourTable
)
SELECT
(mean_col1-mean_col2)/sqrt(var_col1/NULLIF(count_col1,0)+var_col2/NULLIF(count_col2,0))
FROM
cte;
Go to Top of Page

jimris23
Starting Member

2 Posts

Posted - 2014-10-24 : 20:30:45
Thanks James, I will try that method out. But I was wondering if you could help me out and give me an example of a procedure too? I thought that it might be hard doing it via a udf and so a procedure would have been the fall back.
Thanks in advance!
Jimmy Mirchandani
Go to Top of Page
   

- Advertisement -