BiqQuery - 使用max函数选择值

时间:2015-05-31 19:24:27

标签: sql max google-bigquery

我有这种表(例如..在我的情况下,这个表有超过60000条记录)

enter image description here

我想知道如何制作一个选择以获得更高费率的movieId(或songId)的性别比率?

我这样查询:

select * from
(
SELECT avg(rating)as rating, movieID,'M' as gender FROM [OF7.TEST] 
where gender = 'M'
group by movieID
) ,
(
SELECT avg(rating)as rating, movieID,'F' as gender FROM [OF7.TEST] 
where gender = 'F'
group by movieID
) 
order by movieId

获得这个结果之王:

enter image description here

问题是: 性别之间是否存在差异,哪些 性别比率较高的电影,这种差异是否显着?

  • 有没有办法修改查询以获得某些songId / movieId和性别的最大(费率)?

谢谢

1 个答案:

答案 0 :(得分:2)

我之前从未使用过bigquery,但是这样的事情应该有效:

SELECT  movieID,
        CASE
            WHEN F_rate >= M_rate THEN F_rate
            ELSE M_rate
        END max_rating,
        CASE
            WHEN F_rate > M_rate THEN 'Females Rated it Higher'
            WHEN F_rate < M_rate THEN 'Males Rated it Higher'
            ELSE 'Rated Equal'
        END AS who_rated_it_higher,
        ABS(F_rate - M_rate) --absolute value of difference

FROM
(
    SELECT  movieID,
            AVG(CASE WHEN gender = 'F' THEN rating END) AS F_rate,
            AVG(CASE WHEN gender = 'M' THEN rating END) AS M_rate
    FROM CTE
    GROUP BY MovieID
) AS A

如果您有任何问题或需要其他任何问题,请告诉我。