BigQuery - 计算列的计数

时间:2018-04-27 18:49:55

标签: google-bigquery

一般来说,SQL和BigQuery上的新手。如何计算BigQuery中的列数?从代码示例中可以看出,查询将appName的计数返回为WhitelistNames,但我希望获得WhitelistNames的计数。

SELECT
COUNT(appName) AS WhitelistNames,
  bridgeToken
FROM (
  SELECT
    bridgeToken,
    appName
  FROM
    [DB]
  GROUP BY
    bridgeToken,
    appName )
GROUP BY
  bridgeToken
ORDER BY
  WhitelistNames DESC

当前查询返回:

Row UniquebridgeToken   WhitelistEntries    
1   11111               5         
2   22222               13    
3   33333               3     
4   44444               3
5   55555               3     

但我想计算下面的UniquebridgeToken的出现次数。在此先感谢。:

Row WhitelistEntries    BridgeCount
1   13                  1
2   5                   1
3   3                   3

2 个答案:

答案 0 :(得分:2)

下面是BigQuery Standard SQL,并根据我如何解释你的问题 - 即: 每个bridgeToken有多少独特的appName以及该网桥的总条目数(行数)

   
#standardSQL
SELECT 
  COUNT(DISTINCT appName) AS WhitelistNames, 
  COUNT(bridgeToken) AS BridgeCount
FROM `project.dataset.your_table`
GROUP BY bridgeToken

答案 1 :(得分:0)

我知道你想要的是计算有多少UniquebridgeToken具有相同数量的WhitelistEntries。我认为你在寻找的是:

WITH nestedQuery AS (SELECT
    appName,
    COUNT(appName) as WhitelistEntries
FROM `project_name.dataset_name.table_name`
GROUP BY 
     price)

SELECT n.WhitelistEntries, COUNT(n.WhitelistEntries) as BridgeCount
FROM nestedQuery as n
GROUP BY n.WhitelistEntries

您可以在此处阅读有关WITH子句的内容:https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#with_clause