2016-07-15 79 views
1


我想根據產品數量來分配排名值(例如,產品1的數量是100,這是最大的應該有排名1,產品2的第二個最高數量應該是80的排名2等)有,但是當我執行下面的查詢時,我得到一個錯誤說 - 字段'rank_'未在表
中找到。請讓我知道我要去哪裏錯了
bigquery中的排名()

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

嘗試以下

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