限制列值重复到前2个

时间:2018-09-27 16:08:33

标签: sql ansi-sql snowflake-datawarehouse

所以我有这个查询:

SELECT 
    Search.USER_ID,
    Search.SEARCH_TERM,
    COUNT(*) AS Search.count
FROM Search

GROUP BY 1,2
ORDER BY 3 DESC

哪个返回的响应如下:

USER_ID   SEARCH_TERM    count
bob       dog            50
bob       cat            45
sally     cat            38
john      mouse          30
sally     turtle         10
sally     lion           5
john      zebra          3
john      leopard        1

我的问题是:我将如何更改查询,以使其仅返回任何给定用户的搜索次数最多的前2个?因此,在上面的示例中,Sally的最后一行将被删除,John的最后一行也将被删除,总共6行;每个用户2个,就像这样:

USER_ID   SEARCH_TERM    count
bob       dog            50
bob       cat            45
sally     cat            38
john      mouse          30
sally     turtle         10
john      zebra          3

1 个答案:

答案 0 :(得分:2)

在SQL Server中,您可以将原始查询放入CTE中,添加ROW_NUMBER()函数。然后在新的主查询中,只需添加一个WHERE子句以限制行号。您的查询看起来像这样:

;WITH OriginalQuery AS
(
    SELECT 
        s.[User_id]
        ,s.Search_Term
        ,COUNT(*) AS 'count'
        ,ROW_NUMBER() OVER (PARTITION BY s.[USER_ID] ORDER BY COUNT(*) DESC) AS rn
    FROM Search s
    GROUP BY s.[User_id], s.Search_Term
)
SELECT oq.User_id
      ,oq.Search_Term
      ,oq.count
FROM OriginalQuery oq
WHERE rn <= 2
ORDER BY oq.count DESC 

编辑:我将SQL Server指定为我在此处使用的dbms,但以上内容应符合ANSI并在Snowflake中工作。