这里我们比较基因表记录如下:首先将基因a和所有基因a,b,c进行比较,例如aa,ab,ac同样需要b然后ba,bb,bc等...... ....
所以这里的匹配结果是n和b是2因为匹配记录是589,822共同基因项,因为b c count是1,因为匹配记录586和所有其他组合它应该是零。
goterm gene auto
--------------------
589 a 1
822 a 2
478 a 3
586 b 4
589 b 5
600 c 6
586 c 7
822 b 8
查询:
select count(*),
x.gene,
x.ng
from (select t.gene,
v.gene as ng
from (select distinct gene
from gene) as t
cross join (select distinct gene from gene) as v) as x
left join (select (g.gene),(n.gene) as ng from gene g
join gene n on n.goterm=g.goterm where g.auto<n.auto ) as y on y.gene = x.ng
and y.ng = x.gene
group by x.gene,x.ng
最后,上述查询的输出是:
count gene gene
1 a a
2 b a
1 c a
1 a b
1 b b
1 c b
1 a c
1 b c
1 c c
但输出必须是:
count gene gene
0 a a
2 b a
0 c a
0 a b
0 b b
1 c b
0 a c
0 b c
0 c c
答案 0 :(得分:0)
不确定你的逻辑究竟是如何工作的(尤其是auto
上的比较),但是这个查询产生了你正在寻找的结果:
declare @t table (goterm int, gene char(1), auto int identity)
insert @t
select 589, 'a'
union select all 822, 'a'
union select all 478, 'a'
union select all 586, 'b'
union select all 589, 'b'
union select all 600, 'c'
union select all 586, 'c'
union select all 822, 'b'
select t1.gene
, t2.gene
, (
select COUNT(*)
from @t t3
join @t t4
on t3.goterm = t4.goterm
and t3.auto > t4.auto
where t3.gene = t1.gene
and t4.gene = t2.gene
) as Total
from (
select distinct gene
from @t
) t1
cross join
(
select distinct gene
from @t
) t2
order by
t2.gene
, t1.gene
打印:
gene gene Total
a a 0
b a 2
c a 0
a b 0
b b 0
c b 1
a c 0
b c 0
c c 0
t1
和t2
用于创建基因组合矩阵。 Total
查询会查找该组合的匹配数。 t3.auto > t4.auto
条件让我感到困惑,因此您可能需要仔细检查。
答案 1 :(得分:0)
select combo1.gene, combo2.gene, count (gene2.goterm)
from (select distinct gene from gene) combo1
cross join (select distinct gene from gene) combo2
join gene gene1 on combo1.gene = gene1.gene
left join gene gene2 on combo2.gene = gene2.gene
and gene1.goterm = gene2.goterm and gene1.auto < gene2.auto
group by combo1.gene, combo2.gene
order by combo1.gene, combo2.gene
编辑:意识到我毕竟不需要DISTINCT。
答案 2 :(得分:0)
尝试在查询中将count(*)
更改为count(y.gene)
。