我尝试使用旧版SQL构建查询,我需要使用两个不同的自定义维度。这是我到目前为止所写的内容:
SELECT
count_distinct(hits.customDimensions.value) AS Articles,
DATE(timestamp(hits.customDimensions.value)) AS date_,
FROM
[storied-toolbox-113108:112162023.ga_sessions_20161104]
WHERE
hits.customDimensions.index=4 AND
DATE(timestamp(hits.customDimensions.value)) > DATE(DATE_ADD(TIMESTAMP(CURRENT_DATE()), -7, "DAY"))
GROUP BY
date_
但是我想在customDimensions索引10上应用count_distinct
函数,并在customDimensions索引4上应用date
函数。
我该怎么做?
谢谢!
答案 0 :(得分:3)
您可以通过将SELECT语句拆分为两个来完成此操作:
SELECT count_distinct(hits.customDimensions.value) AS Articles
FROM [storied-toolbox-113108:112162023.ga_sessions_20161104]
WHERE hits.customDimensions.index=10
DATE(timestamp(hits.customDimensions.value)) AS date_
FROM [storied-toolbox-113108:112162023.ga_sessions_20161104]
WHERE hits.customDimensions.index=4 AND
DATE(timestamp(hits.customDimensions.value)) > DATE(DATE_ADD(TIMESTAMP(CURRENT_DATE()), -7, "DAY"))
然后,您可以将结果合并为两个单独的列:
SELECT * FROM (SELECT ...), (SELECT ...)
或单个(虽然需要是相同的类型,因此是字符串转换)
SELECT COALESCE(date_, Articles)
FROM (SELECT STRING(...) as date_ ...)
, (SELECT STRING(...) as Articles ...)
答案 1 :(得分:1)
尝试一下:
SELECT date,
(IF(hits.customDimensions.index=3,hits.customDimensions.value,NULL)),
(IF(hits.customDimensions.index=9,hits.customDimensions.value,NULL))
FROM TABLE_DATE_RANGE(
[YourTable.ga_sessions_],
DATE_ADD(CURRENT_TIMESTAMP(), -30, 'DAY'),
CURRENT_TIMESTAMP())
LIMIT 1000