-2

我试图在表中的以下数据集上找到 CHI-SQUARE TEST。我正在尝试我的这个查询来找到 CHI-SQUARE TEST:

 SELECT sessionnumber, sessioncount, timespent,
 (dim1.cnt * dim2.cnt * dim3.cnt)/(dimall.cnt*dimall.cnt) as expected
 FROM (SELECT sessionnumber, SUM(cast(cnt as bigint)) as cnt
 FROM d3
 GROUP BY sessionnumber) dim1 CROSS JOIN
 (SELECT sessioncount, SUM(cast(cnt as bigint)) as cnt
 FROM d3
 GROUP BY sessioncount) dim2 CROSS JOIN
 (SELECT timespent, SUM(cast(cnt as bigint)) as cnt
 FROM d3
 GROUP BY timespent) dim3 CROSS JOIN
 (SELECT SUM(cast(cnt as bigint)) as cnt FROM d3) dimall

样本数据为:

sessionnumber   sessioncount    timespent       cnt
1                  17               28          45
2                  22               8           30
3                  1                1           2
4                  1                1           2
5                  8               111          119
6                  8                65          73
7                  11               5           16
8                  1                1           2
9                  62               64          126
10                 6                42          48

但它给我的卡方检验值输出错误,它给出的输出是:

sessionnumber   sessioncount    timespent   expected
1                  23               1          0
2                  23               1          0
3                  23               1          0
4                  23               1          0
5                  23               1          0
6                  23               1          0
7                  23               1          0
8                  23               1          0
9                  23               1          0
10                 23               1          0

我已经尽力了,并搜索了很多关于这个问题的信息。请帮我一个忙,请解决问题!提前致谢!

4

2 回答 2

2

因为您已经在计算中进行了强制转换,所以您不妨强制转换为float而不是bigint

 SELECT sessionnumber, sessioncount, timespent,
 (dim1.cnt * dim2.cnt * dim3.cnt)/(dimall.cnt*dimall.cnt) as expected
 FROM (SELECT sessionnumber, SUM(cast(cnt as float)) as cnt
 FROM d3
 GROUP BY sessionnumber) dim1 CROSS JOIN
 (SELECT sessioncount, SUM(cast(cnt as float)) as cnt
 FROM d3
 GROUP BY sessioncount) dim2 CROSS JOIN
 (SELECT timespent, SUM(cast(cnt as float)) as cnt
 FROM d3
 GROUP BY timespent) dim3 CROSS JOIN
 (SELECT SUM(cast(cnt as float)) as cnt FROM d3) dimall;

float有大约 16 位的精度,所以它应该足以计算已知宇宙中任何合理数量的物体。

于 2013-08-04T13:47:56.547 回答
2

整数数学,将 diall.cnt 转换为小数或数字或执行以下操作

/(dimall.cnt* 1.00)* (dimall.cnt * 1.00)

另一个例子来解释实际发生的事情

select 3/2  -- output = 1, integer math, result is an integer

select 3/2.00  -- output = 1.50
于 2013-08-04T13:14:03.077 回答