Google BigQuery:SELECT列表表达式引用列user_id,它既不是在[8:5]分组也不是聚合

时间:2018-06-18 11:22:17

标签: sql google-bigquery

我有2个数据集。所有生病的患者之一(endo-2)和一个特殊的患者组之一也存在于endo-2中称为“xp-56”

我一直在尝试运行此查询,但我不确定它为什么不起作用。我想在属于xp-56表的那些患者的endo-2中进行3列计数。

这是我一直使用的代码,出现以下错误

SELECT列表表达式引用列user_id,它既不在[8:5]

分组也不聚合

我该如何解决这个问题,所以我再也不会犯同样的错误了!

SELECT
 Virus_Exposure,
 Medical_Delivery,
 Number_of_Site
 FROM 
 (
  SELECT
  medical_id,
  COUNT(DISTINCT Virus_id) AS Virus_Exposure,
  COUNT(EndoCrin_id) AS Medical_Delivery,
  COUNT (site_id_clinic) AS Number_of_Site
   FROM
   `endo-2`
    WHERE
   _PARTITIONTIME BETWEEN TIMESTAMP("2017-12-15")
    AND TIMESTAMP("2018-01-10")) AS a

RIGHT JOIN 

(
 SELECT
 medical_id
 FROM
 `xp-56`
 ORDER BY
   medical_id DESC) AS b

ON
a.medical_id=b.medical_id

GROUP BY
medical_id

为什么表格中的medical_id不起作用?

1 个答案:

答案 0 :(得分:0)

为什么不这样做?

SELECT e.medical_id,
       COUNT(DISTINCT e.Virus_id) AS Virus_Exposure,
       COUNT(e.EndoCrin_id) AS Medical_Delivery,
       COUNT(e.site_id_clinic) AS Number_of_Site
FROM `endo-2` e JOIN
     `xp-56` x
     ON x.medical_id = e.medical_id
WHERE e._PARTITIONTIME BETWEEN TIMESTAMP("2017-12-15") AND TIMESTAMP("2018-01-10")
GROUP BY e.medical_id;