SQL如果组遵循某些规则,如何查找多个组的平均值

时间:2017-12-31 01:39:03

标签: sql google-bigquery bigdata

这是我目前的代码:

SELECT
AVG(famTotal)

FROM `OmniHealth.new2015Data`, (
SELECT
SUM( TOTEXP15 ) as famTotal
FROM `OmniHealth.new2015Data` 

GROUP BY DUID
)
WHERE BMINDX53 BETWEEN 0 AND 25 AND
ADSMOK42 = -1 AND
FCSZ1231 = 7

我想要做的是找出每个家庭的平均成本,其家庭所有成员的BMI在0到25之间,不吸烟,家庭是任意大小。

支出数据是按人数计算的,所以我试图根据所有人的“住宅单位ID”(DUID)进行求和,然后平均每个人的总数家庭,只要家庭只有我在上一段中陈述的属性。

感谢您的回复!我是SQL的新手。

2 个答案:

答案 0 :(得分:1)

以下是BigQuery Standard SQL

   
#standardSQL
SELECT DUID, AVG(TOTEXP15) AS famAverage
FROM `OmniHealth.new2015Data` 
GROUP BY DUID
HAVING MIN(BMINDX53) >=0 AND MAX(BMINDX53) <=25
AND MIN(ADSMOK42) = -1 AND MAX(ADSMOK42) = -1
AND MIN(FCSZ1231) = 7 AND MAX(FCSZ1231) = 7

答案 1 :(得分:1)

考虑加入两个与计数匹配的聚合查询派生表,以便将所有家庭成员与具有特定条件的所有家庭成员对齐。

SELECT AVG(t1.famTotal) as famTotal

FROM
  (SELECT DUID, Count(*) As GrpCount, SUM(TOTEXP15) as famTotal
   FROM `OmniHealth.new2015Data` 
   GROUP BY DUID) As t1

INNER JOIN

  (SELECT DUID, Count(*) As GrpCount
   FROM `OmniHealth.new2015Data` 
   WHERE BMINDX53 BETWEEN 0 AND 25 
     AND ADSMOK42 = -1 
     AND FCSZ1231 = 7
   GROUP BY DUID) As t2

ON t1.DUID = t2.DUID AND t1.GrpCount = t2.GrpCount