窗口函数中的基本分组

时间:2017-10-11 10:06:03

标签: sql google-bigquery

我第一次使用窗口函数。我有一个基本的窗口函数,我想用Medium对结果进行分组,当我这样做时,我得到了错误:

Error: SELECT list expression references totals.visits which is neither grouped nor aggregated at [1:12]

在我看来,我总结了第一行的总数。访问量,我在这里缺少什么?我希望看到按国家/地区划分的访问总次数,例如:

VISITS          COUNTRY
1500            United Kingdom
750             Ireland
etc.

这是我的问题:

 SELECT
        SUM(totals.visits) OVER(PARTITION BY geoNetwork.country
                                ORDER BY geoNetwork.country) AS Visits_by_Medium,
        trafficSource.medium AS Medium
    FROM `xxx.ga_sessions_20171010`
    GROUP BY Medium

1 个答案:

答案 0 :(得分:1)

我想你只想要一个简单的GROUP BY查询:

SELECT
    SUM(totals.visits) AS VISITS,
    geoNetwork.country
FROM xxx.ga_sessions_20171010
GROUP BY
    geoNetwork.country;

修改

运行以下查询以查看窗口函数将返回的总和:

SELECT
    SUM(totals.visits) OVER(PARTITION BY geoNetwork.country
                            ORDER BY geoNetwork.country) AS Visits_by_Medium,
    trafficSource.medium AS Medium
FROM xxx.ga_sessions_20171010;