列出不同的名称Big Query

时间:2017-11-16 16:22:56

标签: google-bigquery

我在Big Query中有以下表格:

|event  |count
|event1 |1
|event2 |2
|event1 |2
|event2 |1 
|event3 |4 
|event1 |3 
|event3 |5 
|event4 |5 
|event5 |4 
|event1 |3 
|event6 |5 
|event4 |5 

包含6个不同的事件:

COUNT(DISTINCT event) AS Dist_event_Count = 6

现在我要列出不同的事件。我应该做什么查询来获得以下输出?

|event1 
|event2     
|event3          
|event4           
|event5
|event6

2 个答案:

答案 0 :(得分:0)

以下是BigQuery Standard SQL

  
#standardSQL
WITH `project.dataset.yourTable` AS (
  SELECT 'event1' event, 1 cnt UNION ALL
  SELECT 'event2', 2 UNION ALL
  SELECT 'event1', 2 UNION ALL
  SELECT 'event2', 1 UNION ALL
  SELECT 'event3', 4 UNION ALL
  SELECT 'event1', 3 UNION ALL
  SELECT 'event3', 5 UNION ALL
  SELECT 'event4', 5 UNION ALL
  SELECT 'event5', 4 UNION ALL
  SELECT 'event1', 3 UNION ALL
  SELECT 'event6', 5 UNION ALL
  SELECT 'event4', 5 
)
SELECT DISTINCT event
FROM `project.dataset.yourTable`

或者 - 你可以使用GROUP BY所以在这种情况下你也可以聚合各自的计数如下

#standardSQL
WITH `project.dataset.yourTable` AS (
  SELECT 'event1' event, 1 cnt UNION ALL
  SELECT 'event2', 2 UNION ALL
  SELECT 'event1', 2 UNION ALL
  SELECT 'event2', 1 UNION ALL
  SELECT 'event3', 4 UNION ALL
  SELECT 'event1', 3 UNION ALL
  SELECT 'event3', 5 UNION ALL
  SELECT 'event4', 5 UNION ALL
  SELECT 'event5', 4 UNION ALL
  SELECT 'event1', 3 UNION ALL
  SELECT 'event6', 5 UNION ALL
  SELECT 'event4', 5 
)
SELECT event, SUM(cnt) cnt
FROM `project.dataset.yourTable`
GROUP BY event

答案 1 :(得分:0)

event_dataset.event_data LIMIT 1000中选择DISTINCT事件;
     使用标准SQL,您可以使用反引号event_dataset.event_data