计算每一行的频率

时间:2016-09-27 15:36:14

标签: sql postgresql-9.5

我正在尝试计算每行中元素的频率,我将解释: 我从包含一些元素的表中选择,例如“pos,chr,ref,alt,id_disease”。

从这些我必须提取我的参考频率,alt:

num_occurrencies_of(ref='A' and alt='C')/total number of rows

使用此查询我几乎不接近我的目标,事实上id无法正确计算它返回的频率a constant

SELECT pos, chr, upper(ref||' '||alt) AS refalt, id_disease AS lvl15, t1.tot_var, t1.freq
FROM varianti 
JOIN ( SELECT count(*) AS tot_var,(count(*)::numeric / sum(count(*)) over ()) as freq
       FROM varianti)t1 ON TRUE
WHERE length(ref)=1 AND length(alt)=1 AND chr similar to 'chr[\d X Y]*'

我想要的只是检索这样的数据:

chr pos refalt lvl15 freq tot_var
1   120  AT     15    0.3  1000
1   150  CG     30    0.01 1000

tot_var =计算我需要的行总数(不能是1我计算每一行!)

ref和alt都可以在每个排列中使用这些值(A,T,C,G),AA,AT,TA,TC,CT等。

我的代码中缺少什么?

告诉我你是否想要更多信息

varianti的例子:

chr pos ref alt id_disease
chr1 152 A   C    15
chr3 487 T   T    74

这是我的查询的输出:

pos          chr    refalt  lvl15   tot_var freq
124338543   chr11   G A      69      1     0.000000677833751782702767
124338595   chr11   C T      28      1      0.000000677833751782702767
124361862   chr11   C .      53      1     0.000000677833751782702767
124361899   chr11   T A      20      1     0.000000677833751782702767

1 个答案:

答案 0 :(得分:1)

根据您提供的信息

SELECT DISTINCT chr, pos, 
upper(ref||' '||alt) AS refalt, id_disease AS lvl15, 
SUM(CASE WHEN (ref == 'A' AND alt == 'C')THEN 1 ELSE 0 END)/COUNT(*) AS 'freq', 
COUNT(*) AS 'tot_var' 
FROM varianti

我仍然不确定'tot_var'是什么。获取实际数据样本以及该数据样本本身的预期输出将非常有用。

编辑1:获取数据集中每对夫妇的频率

SELECT DISTINCT upper(ref||' '||alt) AS refalt, 
COUNT(chr)/COUNT(*) AS 'freq' 
FROM varianti 
GROUP BY refalt

编辑2:根据要求更新了查询

SELECT varianti.chr, varianti.pos, 
upper(varianti.ref||' '||varianti.alt) AS refalt, varianti.id_disease AS lvl15, COUNT(*) AS 'tot_var', 
FROM varianti
JOIN 
( SELECT DISTINCT upper(ref||' '||alt) AS refalt, 
  COUNT(chr)/COUNT(*) AS 'freq' 
  FROM varianti 
  GROUP BY refalt 
) refalt_table ON refalt_table.refalt = varianti.refalt

编辑3:基于错误更新了查询

SELECT chr, pos, upper(ref||' '||alt) as refalt, id_disease AS lvl15, refalt_table.freq as 'freq', (SELECT COUNT(*) FROM varianti tot where tot.pos = v.pos) as 'tot_var'
FROM varianti v
LEFT JOIN 
( SELECT DISTINCT UPPER(ref) as 'ref',UPPER(alt) as 'alt', 
  COUNT(pos)/(SELECT COUNT(*) FROM varianti vcount) AS 'freq' 
  FROM varianti
  GROUP BY ref,alt
) refalt_table ON refalt_table.ref = v.ref and refalt_table.alt = v.alt