为相关计算分组一系列值

时间:2017-03-18 14:44:29

标签: sql postgresql

下面有table1,它针对两个不同的团队meas1在不同位置locid捕获一些衡量标准team - 我需要计算meas1分布之间的相似度{1}}两个组中每个位置的值{1}} gp1gp2。为了测量相似性,我打算使用corr函数,该函数给出两个分布之间的相关系数。

表1

+-------+--------+--------+--------+
| locid |  make  |  meas1 |  team  |
+-------+--------+--------+--------+
|  1111 |  make1 |      1 |  gp1   |
|  1111 |  make1 |      2 |  gp1   |
|  1111 |  make2 |      2 |  gp2   |
|  1111 |  make3 |      1 |  gp2   |
|  1112 |  make1 |      2 |  gp1   |
|  1112 |  make2 |      2 |  gp2   |
|  1112 |  make3 |      2 |  gp2   |
|  1113 |  make1 |      2 |  gp1   |
|  1113 |  make2 |      2 |  gp2   |
|  1113 |  make2 |      3 |  gp2   |
|  1113 |  make3 |      1 |  gp2   |
+-------+--------+--------+--------+

我的尝试

select locId,
    corr((select meas1 from table1 where team = 'gp1'), (select meas1 from table1 where team = 'gp2') ) as corr_meas1
    group by locId;

Rextester here

1 个答案:

答案 0 :(得分:0)

您需要自己加入表格,以便meas1个团队形成可比较的对。像

这样的东西
select t1.locId,
    corr(t1. meas1, t2.meas1)
    from table1 t1
    join table1 t2 on t1.team = 'gp1' and t2.team = 'gp1' and t1.locId =  t2.locId 
      and t1.make = t2.make 
    group by t1.locId;

根据需要调整连接条件。