如何在Bigquery中计算有多少设备连接到模型?

时间:2017-06-29 05:56:30

标签: count google-bigquery

我当前正在运行一个选择不同模型类型的查询,按序列号对其进行排序,并将该设备用于的连接时间相加。我还想添加它来计算每个模型使用的设备数量。设备由DeviceID表示,它是一个字符串

Select Serial, MAX(createdAt),
SUM(ConnectionTime) as Total 
FROM [dataworks-356fa:FirebaseArchive.testf] 
Where Model In
  (Select Model 
  From [dataworks-356fa:FirebaseArchive.testf]
  Where Model == "BlueBox-pH")
Group BY Serial
LIMIT 1000
;

1 个答案:

答案 0 :(得分:2)

您可以使用COUNT(DISTINCT ...)但请确保使用标准SQL,因为旧版SQL不会为您提供准确的计数。例如,

#standardSQL
SELECT
  Serial,
  MAX(createdAt) AS Latest,
  SUM(ConnectionTime) as Total,
  COUNT(DISTINCT Model) AS Devices
FROM `dataworks-356fa.FirebaseArchive.testf`
WHERE Model = "BlueBox-pH"
GROUP BY Serial
ORDER BY Serial
LIMIT 1000;

由于过滤器仅包含特定型号,因此每个序列号的计数只会为1。