我已经尝试过,
dense_rank()over(TEAM_CD分区)
我想在“真实”来之前对TEAM_CD进行排名。 喜欢这个!!
TEAM_CD RANK
-------------------
false 1
true 2
false 1
false 2
false 3
true 4
false 1
true 2
我是用户Bigquery,Legacy SQL !! 帮我!!!! (我不擅长写英文......)
答案 0 :(得分:1)
正如Elliott所说 - 你需要一些方法来定义订单。您的表中应该有一些列用于定义它。它可以是orderable类型的任何列
假设您的表中有一个pos
列 - 下面的查询将执行您所要求的内容(适用于BigQuery Standard SQL)
#standardSQL
SELECT
pos,
team_cd,
DENSE_RANK() OVER(PARTITION BY grp ORDER BY pos) AS RANK
FROM (
SELECT
pos,
team_cd,
COUNTIF(team_cd) OVER(ORDER BY pos ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS grp
FROM `project.dataset.table`
)
-- ORDER BY pos
您可以使用问题中的dummydata进行测试/播放,如下所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 AS pos, FALSE AS team_cd UNION ALL
SELECT 2, TRUE UNION ALL
SELECT 3, FALSE UNION ALL
SELECT 4, FALSE UNION ALL
SELECT 5, FALSE UNION ALL
SELECT 6, TRUE UNION ALL
SELECT 7, FALSE UNION ALL
SELECT 8, TRUE
)
SELECT
pos,
team_cd,
DENSE_RANK() OVER(PARTITION BY grp ORDER BY pos) AS RANK
FROM (
SELECT
pos,
team_cd,
COUNTIF(team_cd) OVER(ORDER BY pos ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS grp
FROM `project.dataset.table`
)
ORDER BY pos
如果由于某种原因你仍然使用BigQuery Legacy SQL,你可以在下面使用(但考虑moving to Standard SQL - BigQuery团队强烈建议使用它)
#legacySQL
SELECT
pos,
team_cd,
DENSE_RANK() OVER(PARTITION BY grp ORDER BY pos) AS RANK
FROM (
SELECT pos, team_cd, IFNULL(grp, 0) grp
FROM (
SELECT
pos,
team_cd,
SUM(team_cd) OVER(ORDER BY pos ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS grp
FROM [project:dataset.table]
)
)
ORDER BY pos