大查询在多个列中的前N个结果

时间:2019-05-17 23:33:19

标签: sql google-bigquery pivot pivot-table

假设我有以下记录:

id   studio        movie
1    fox           avatar
2    paramount     transformers
etc.

我想按标题数量查询前2个制片厂,其中前3个电影按字母顺序排序。结果看起来像这样:

studio (top 2 by title cnt)        title (top 3 alphabetically)
fox                                avatar
fox                                avatar2
fox                                avatar3
sony                               ace in the hole
sony                               antonio
sony                               spider-man

我将如何查询以获取此信息?到目前为止,我有类似的内容,但是我不确定最后如何进行排序:

select * from `table` where studio in (
    SELECT studio FROM `table` group by studio order by count(*) desc limit 3
) 

2 个答案:

答案 0 :(得分:1)

您将需要使用窗口函数(例如ROW_NUMBER)和聚合的某种组合。

这是一种可能的方法(我组成了表标识符,因此您必须插入自己的表标识符):

WITH studio_counts AS
    (

        SELECT
            studio
            ,ROW_NUMBER() OVER(ORDER BY COUNT(studio) DESC) As rownum
        FROM
            project.dataset.movies
        GROUP BY
            studio
    )

SELECT
    mc.studio
    ,mc.movie_title
FROM
    (
        SELECT
            m.studio
            ,m.movie_title
            ,ROW_NUMBER() OVER(PARTITION BY m.studio ORDER BY m.movie_title) AS rownum2
        FROM
            studio_counts AS sc

            INNER JOIN project.dataset.movies AS m
            ON sc.studio = m.studio
        WHERE
            sc.rownum < 3
    ) AS mc
WHERE
    mc.rownum2 < 4

答案 1 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
SELECT studio, title
FROM (
  SELECT studio, ARRAY_AGG(movie ORDER BY movie LIMIT 3) movies
  FROM `project.dataset.table`
  GROUP BY studio
  ORDER BY COUNT(movie) DESC
  LIMIT 2
), UNNEST(movies) title