SQL根据解析函数过滤查询结果
SQL filter query results based on analytic function
我想找到一种有效的方法来过滤 SQL 中的 RANK() OVER
函数。
我有以下查询:
SELECT
base.ITEM_SKU_NBR,
RANK() OVER (ORDER BY SUM(base.NET_SLS_AMT) DESC) AS SLS_rank,
RANK() OVER (ORDER BY COUNT(DISTINCT base.txn_id) DESC) AS txn_rank
FROM
`my_table` base
GROUP BY
1
哪个returns这个结果集:
现在我想筛选 SLS_rank
< 10 或 txn_rank
< 10 的项目。理想情况下,我想在 HAVING
中执行此操作子句,像这样:
SELECT
base.ITEM_SKU_NBR,
RANK() OVER (ORDER BY SUM(base.NET_SLS_AMT) DESC) AS SLS_rank,
RANK() OVER (ORDER BY COUNT(DISTINCT base.txn_id) DESC) AS txn_rank
FROM
`my_table` base
GROUP BY
1
HAVING
SLS_rank < 10 OR txn_rank < 10
但是 bigquery 抛出一个错误:
Column SLS_rank contains an analytic function, which is not allowed in HAVING clause at [9:8]
我能想到的唯一选择是将它创建为一个单独的 table 并从那里选择,但这似乎不太好。关于如何执行此操作还有其他想法吗?
SELECT * FROM (
SELECT
base.ITEM_SKU_NBR,
RANK() OVER (ORDER BY SUM(base.NET_SLS_AMT) DESC) AS SLS_rank,
RANK() OVER (ORDER BY COUNT(DISTINCT base.txn_id) DESC) AS txn_rank
FROM `my_table` base
GROUP BY 1
)
WHERE SLS_rank < 300 OR txn_rank < 300
2021 年 6 月更新。
BigQuery 宣布支持 the 10th of May, 2021 上的 QUALIFY 子句。
The QUALIFY clause filters the results of analytic functions. An analytic function is required to be present in the QUALIFY clause or the SELECT list.
您可以通过以下方式通过 QUALIFY 实现您的需求:
SELECT
base.ITEM_SKU_NBR,
RANK() OVER (ORDER BY SUM(base.NET_SLS_AMT) DESC) AS SLS_rank,
RANK() OVER (ORDER BY COUNT(DISTINCT base.txn_id) DESC) AS txn_rank
FROM `my_table` base
GROUP BY 1
QUALIFY SLS_rank < 10 OR txn_rank < 10
在 documentation 中查找更多示例。
我想找到一种有效的方法来过滤 SQL 中的 RANK() OVER
函数。
我有以下查询:
SELECT
base.ITEM_SKU_NBR,
RANK() OVER (ORDER BY SUM(base.NET_SLS_AMT) DESC) AS SLS_rank,
RANK() OVER (ORDER BY COUNT(DISTINCT base.txn_id) DESC) AS txn_rank
FROM
`my_table` base
GROUP BY
1
哪个returns这个结果集:
现在我想筛选 SLS_rank
< 10 或 txn_rank
< 10 的项目。理想情况下,我想在 HAVING
中执行此操作子句,像这样:
SELECT
base.ITEM_SKU_NBR,
RANK() OVER (ORDER BY SUM(base.NET_SLS_AMT) DESC) AS SLS_rank,
RANK() OVER (ORDER BY COUNT(DISTINCT base.txn_id) DESC) AS txn_rank
FROM
`my_table` base
GROUP BY
1
HAVING
SLS_rank < 10 OR txn_rank < 10
但是 bigquery 抛出一个错误:
Column SLS_rank contains an analytic function, which is not allowed in HAVING clause at [9:8]
我能想到的唯一选择是将它创建为一个单独的 table 并从那里选择,但这似乎不太好。关于如何执行此操作还有其他想法吗?
SELECT * FROM (
SELECT
base.ITEM_SKU_NBR,
RANK() OVER (ORDER BY SUM(base.NET_SLS_AMT) DESC) AS SLS_rank,
RANK() OVER (ORDER BY COUNT(DISTINCT base.txn_id) DESC) AS txn_rank
FROM `my_table` base
GROUP BY 1
)
WHERE SLS_rank < 300 OR txn_rank < 300
2021 年 6 月更新。
BigQuery 宣布支持 the 10th of May, 2021 上的 QUALIFY 子句。
The QUALIFY clause filters the results of analytic functions. An analytic function is required to be present in the QUALIFY clause or the SELECT list.
您可以通过以下方式通过 QUALIFY 实现您的需求:
SELECT
base.ITEM_SKU_NBR,
RANK() OVER (ORDER BY SUM(base.NET_SLS_AMT) DESC) AS SLS_rank,
RANK() OVER (ORDER BY COUNT(DISTINCT base.txn_id) DESC) AS txn_rank
FROM `my_table` base
GROUP BY 1
QUALIFY SLS_rank < 10 OR txn_rank < 10
在 documentation 中查找更多示例。