检索固定结果-BigQuery标准SQL

时间:2018-08-25 12:05:47

标签: sql google-bigquery flatten

bigQuery中有以下标准SQL查询:

auto

返回的表是平坦的,除了生成的基因型字段(为此,对于返回的每一行都有两个记录)。我想返回一个平面表,其中每行的两个基因型值都转换为两个新列(等位基因1和等位基因2),但正在努力寻找正确的方法。任何指针都很棒

table result

2 个答案:

答案 0 :(得分:1)

您的问题有点难以理解。通常,最好的方法是使用UNNEST()的子查询。我不太清楚您的数据模型(查询和样本数据不一致)。但是,是这样的:

SELECT . . . ,
       (SELECT MAX(call.name)
        FROM UNNEST(call) genotype
        WHERE call.genotype = 0
       ) as genotype_0,
       (SELECT MAX(call.name)
        FROM UNNEST(call) genotype
        WHERE call.genotype = 1
       ) as genotype_1
FROM `mutable`,
     UNNEST(alternate_bases) AS alternate_bases,
     UNNEST(alternate_bases.CSQ_VT) AS alternate_bases_CSQ_VT
WHERE call.name = 'sample name'
      CLIN_SIG = 'pathogenic'
LIMIT 100

答案 1 :(得分:0)

我很感谢我也会尝试的答案-很抱歉,这个问题不太清楚。我确实提出了解决方案,尽管我不知道它是否有效。

SELECT
  Chr,
  start_position,
  reference_bases,
  call.name,
  call.genotype[OFFSET(0)] as all1,
  call.genotype[OFFSET(1)] as all2,
  alternate_bases.alt,
  alternate_bases_CSQ_VT.*
FROM
  `mutable`,
  UNNEST(call) AS call,
  UNNEST(call.genotype) AS genotype,
  UNNEST(alternate_bases) AS alternate_bases,
  UNNEST(alternate_bases.CSQ_VT) AS alternate_bases_CSQ_VT
WHERE
  call.name = "sample name"
  AND CLIN_SIG = "pathogenic"
  AND genotype > 0
LIMIT
  100