在bigquery中排名()

时间:2016-07-15 16:09:45

标签: google-bigquery


我试图根据产品的数量来分配等级值(例如,产品1的数量为100,其中max应该具有等级1,产品2的第二高数量应该具有等级2等等)但是当我执行时在下面的查询我得到一个错误说 - 表'中找不到字段'rank_'。请让我知道我哪里出错了

SELECT
  ProdName,
  NoofProds,
  rank_
FROM (
  SELECT
    ProdName,
    COUNT(ProdName) AS NoofProds,
    RANK() OVER (PARTITION BY ProdName ORDER BY NoofProds DESC) AS rank_
  FROM
    [prodtable]
  WHERE
    (STRFTIME_UTC_USEC(Timestamp,"%Y%m%d")) = (STRFTIME_UTC_USEC(DATE_ADD(CURRENT_TIMESTAMP(), -1, "day"), "%Y%m%d"))
  GROUP BY
    1,
    3
  ORDER BY
    2 DESC)
GROUP BY
  1,
  2,
  3
ORDER BY
  rank_ DESC

1 个答案:

答案 0 :(得分:2)

尝试以下

SELECT
  ProdName,
  NoofProds,
  RANK() OVER(ORDER BY NoofProds DESC) AS rank_ 
FROM
(
  SELECT
    ProdName,
    COUNT(ProdName) AS NoofProds 
  FROM [prodtable]
  WHERE (STRFTIME_UTC_USEC(Timestamp,"%Y%m%d")) = (STRFTIME_UTC_USEC(DATE_ADD(CURRENT_TIMESTAMP(), -1, "day"), "%Y%m%d"))
  GROUP BY 1
)
ORDER BY rank_ DESC