Sql-Server

蜂巢 SQL和rror10025和rr這r10025Error 10025: 表達式不在 GROUP BY 鍵中

  • February 20, 2020

這是我的 SQL 語句:

my_table 包括 10 多個列(例如,day、ip_address、user、request 等),包括字元串和數字。如果超過 20 條記錄,我想根據列“ip_address”按 & HAVING 進行分組。

SELECT day, ip_address, user, request 
FROM my_table
WHERE DAY = current_date()
GROUP BY ip_address
HAVING count(client_ip)>20

我收到此錯誤消息

Error while compiling statement: FAILED: SemanticException [Error 10025]: Expression not in GROUP BY key xxx

但我需要保留所有列。

只要一個 ip 地址超過 20 行,就會返回所有這些行。如果少於 20,則不返回。

WITH cte AS ( SELECT *, COUNT(ip_address) OVER (PARTITION BY ip_address) cnt
             FROM my_table
             WHERE DAY = current_date() )
SELECT *
FROM cte
WHERE cnt > 20;

引用自:https://dba.stackexchange.com/questions/260125