我试图通过收入来获得前3名提供商,并且对于每个提供商,按字母顺序排列该提供商的前2名。这是我必须开始的基本GROUP BY
查询 -
SELECT
provider,
title,
SUM(customer_price)
FROM
[integrated-myth-156821:fintest.m10]
GROUP BY
title,
provider
LIMIT
25
以下是我得到的结果集:
表结构为provider
| title
| customer_price
。
以下是我希望得到的结果的说明:
provider title sum(revenue)
Sony 10000 // Top provider by revenue, limit 3
Sony Avatar 400 // First title alphabetically, limit 2
Sony Titanic 300
Fox 8000
Fox Fresh 12
Fox Mike 800
WB 4500
WB Harry 1000
WB Potter 1200
答案 0 :(得分:2)
以下解决方案是使用BigQuery Standard SQL
#standardSQL
WITH topProviders AS (
SELECT provider, SUM(customer_price) AS revenue
FROM `integrated-myth-156821.fintest.m10`
GROUP BY provider
ORDER BY revenue DESC
LIMIT 3
),
titles AS (
SELECT provider, title, SUM(customer_price) AS revenue
FROM `integrated-myth-156821.fintest.m10`
GROUP BY provider, title
)
SELECT provider, title, revenue
FROM (
SELECT p.revenue AS sort,
STRUCT(p.provider, '' AS title, p.revenue ) AS providers,
ARRAY_AGG(STRUCT(t.provider, title, t.revenue) ORDER BY title LIMIT 2) AS titles
FROM topProviders AS p
JOIN titles AS t
ON p.provider = t.provider
GROUP BY p.provider, p.revenue
), UNNEST(ARRAY_CONCAT([providers], titles))
ORDER BY sort DESC, provider, title
答案 1 :(得分:1)
尝试以下
ROW_NUMBER() OVER(PARTITION BY provider ORDER BY title) AS pos
你可以解释一些查询吗?
此查询实际上是两个查询的联合 其次(在查询的最底部)由收入提供者提供前3名
第一个为前三名中的每个提供商提供前两个标题 - 并且本身就是前三名提供商加入标题收入
以下表达式中的位置
WHERE pos < 3
然后允许仅使用
过滤2个顶级标题Where