group_concat函数删除重复项

时间:2018-01-17 15:46:50

标签: sql google-analytics google-bigquery legacy-sql

在BigQuery中创建了以下查询:


There was an error while trying to load the gem 'bootstrap'.
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes."

作为临时措施,我正在使用having子句对其进行测试(这将在生产中删除)查询输出以下内容:

enter image description here

这一切都很棒并按预期工作,以适当的顺序(平板电脑,平板电脑,平板电脑,手机,桌面)输出设备 - 但是,我想从中删除重复项,因此结果将是“平板电脑,移动设备,桌面“

我尝试使用Unique()函数,这会删除重复项,但订单不会保留,因此输出变为“桌面,移动设备,平板电脑”

任何帮助将不胜感激!

更新

我将查询更新为标准SQL,现在我使用string_agg()函数面临另一个问题:


require "execjs"
ExecJS.eval "'red yellow blue'.split(' ')'"

返回的错误是“具有DISTINCT和ORDER BY参数的聚合函数只能ORDER BY作为函数参数的列”

显然,如果我们从string_agg中删除distinct或order by子句,这是有效的,但我们需要这两个操作。

2 个答案:

答案 0 :(得分:1)

对于更新的问题,以下查询会产生相同的错误:

SELECT age_midpoint, STRING_AGG(DISTINCT country ORDER BY c DESC)
FROM (
  SELECT country, age_midpoint, COUNT(*) c
  FROM `fh-bigquery.stackoverflow.survey_results_2016`
  WHERE age_midpoint IS NOT null
  AND country LIKE '%u%'
  GROUP BY 1, 2
)
GROUP BY 1
ORDER BY 1

这种限制是有道理的,因为一旦你运行DISTINCT,就会失去对你想要提供订单的变量的可见性。

请改为尝试:

#standardSQL
SELECT age_midpoint, ARRAY_TO_STRING(ARRAY(
  SELECT country FROM (SELECT country, c FROM UNNEST(arr) GROUP BY country, c) ORDER BY c DESC
), ',')
FROM (
  SELECT age_midpoint, ARRAY_AGG(STRUCT(country, c)) arr
  FROM (
    SELECT country, age_midpoint, COUNT(*) c
    FROM `fh-bigquery.stackoverflow.survey_results_2016`
    WHERE age_midpoint IS NOT null
    AND country LIKE '%u%'
    GROUP BY 1, 2
  )
  GROUP BY 1
)
ORDER BY 1
LIMIT 1000

(见https://cloud.google.com/bigquery/docs/reference/standard-sql/arrays#creating-arrays-from-subqueries

答案 1 :(得分:0)

感谢Felipe,这是完成的查询!

SELECT
date, value, SUM(visits) visits, STRING_AGG(DISTINCT seqdevice) seqdevice, COUNT(DISTINCT seqdevice) countseqdevice
FROM (
SELECT date, value, visits, ARRAY_TO_STRING(ARRAY(
  SELECT deviceCategory FROM (SELECT deviceCategory, c FROM UNNEST(arr) GROUP BY deviceCategory, c) ORDER BY c DESC
), ',') seqdevice
FROM (
SELECT date, visitStartTime, value, visits, ARRAY_AGG(STRUCT(deviceCategory, c)) arr
FROM (
    SELECT date, visitStartTime, cd.value value, totals.visits visits, device.deviceCategory deviceCategory, COUNT(*) c
    FROM `xxxxxxxxxx`, UNNEST(customDimensions) AS cd
    WHERE cd.index=1 AND STARTS_WITH(cd.value,"hip|")
    GROUP BY 1, 2, 3, 4, 5
    )
GROUP BY 1, 2, 3, 4
)
ORDER BY 2)
 GROUP BY 1, 2
HAVING
  value="hip|7e4fbce9-bbfb-4677-aab0-dcd02851fdb4"

ORDER BY countseqdevice desc