困难的SQL查询全部和任何情况

时间:2017-01-18 10:37:34

标签: sql sqlite

我不知道如何编写SQL查询。

鉴于数据:
几个特征标识符整数 - 比方说1和447。

我正在寻找domain_ids,在其中我将找到分配给特定特征的snp。当域名genomic_posstartend之间的chr_id相同时,SNP就位于域中。有两种情况任何所有

  • 所有案例我正在寻找可以从所有特定特征中找到snps的域名。换句话说,所有特征都在域中具有snp表示。
  • 任何案例我正在寻找可以从任何给定域中找到snps的域名。换句话说,在域中只找到一个特征的snp表示就足够了。

我已经解决了任何案例,但我坚持使用所有案例:(

以下是任何代码:

SELECT DISTINCT domains.domain_id
FROM snps JOIN traits_snps ON snps.snp_id     = traits_snps.snp_id
       JOIN traits      ON traits.trait_id = traits_snps.trait_id
       JOIN chromosomes ON snps.chr_id     = chromosomes.chr_id
       JOIN domains     ON domains.chr_id  = chromosomes.chr_id
WHERE traits.trait_id in (447,1 )   --  <-- HERE GOES TRAIT IDS
   AND genomic_pos BETWEEN start AND end 
   AND domains.chr_id = snps.chr_id;

这是我在SQLite中的数据库。 enter image description here

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您可以使用group byhaving执行您想要的操作:

SELECT d.domain_id
FROM snps s JOIN
     traits_snps ts
     ON s.snp_id = ts.snp_id JOIN
     traits t
     ON t.trait_id = ts.trait_id JOIN
     chromosomes c
     ON s.chr_id  = c.chr_id JOIN
     domains d
     ON d.chr_id = c.chr_id AND
        d.chr_id = s.chr_id  -- JOIN conditions should be in ON clauses
WHERE t.trait_id in (447, 1) AND --  <-- HERE GOES TRAIT IDS
      genomic_pos BETWEEN start AND end 
GROUP BY d.domain_id
HAVING COUNT(DISTINCT t.trait_id) = 2;

答案 1 :(得分:1)

要查找其他表中存在某些数据的域,请使用WHERE EXISTS

任何情况:查找在给定范围内与至少一个特征相关的具有snp的域:

select domain_id
from domains d
where exists
(
  select *
  from snps
  where snps.chr_id = d.chr_id
  and snps.genomic_pos between d.start and d.end 
  and snps.snp_id in (select snp_id from traits_snps where trait_id in (447, 1))
);

所有情况:查找在给定范围内与所有特征相关的具有snp的域:

select domain_id
from domains d
where exists
(
  select *
  from snps
  where snps.chr_id = d.chr_id
  and snps.genomic_pos between d.start and d.end 
  and snps.snp_id in
  (
    select snp_id 
    from traits_snps
    where trait_id in (447, 1)
    group by snp_id
    having count(distinct trait_id) = 2
  )
);