我有一张包含以下结构的表格:
javac
现在我想找到所有用户购买的第三个(基于已购买的数量)不同商品(操作)。那么请你帮我查询一下。抱歉,表格格式不正确。
答案 0 :(得分:1)
我认为你的描述中有类似的东西听起来是正确的。首先按计数选择前3个,按项目分组并按计数降序排序。然后选择该组中的前1个按计数升序排序。请记住,我并不是100%熟悉HiveSQL,但是这个SQL代码应该非常接近标准:
SELECT TOP 1 itemName
FROM (
SELECT TOP 3 itemName, COUNT(*) AS boughtCount
FROM MyTable
WHERE action = 'bought'
GROUP BY itemName
ORDER BY boughtCount DESC
)
ORDER BY boughtCount
编辑:根据评论中的精确度:
编辑2:这是在MSSQL中测试的,可能需要为HiveSQL调整一些语法。
SELECT TOP 1 itemId
FROM (
-- Get the list of the top 3 items that have as many ItemsByUsers entries as distinct userIds
-- in the table, group by item and sort by sum of items bought descending.
SELECT TOP 3 itemId, SUM(boughtCount) AS totalBought
FROM (
-- Get a list of the most bought items by item and user
SELECT itemId, userId, COUNT(*) AS boughtCount
FROM MyTable
WHERE action = 'bought'
GROUP BY itemId, userId
) AS ItemCountByUser
GROUP BY itemId
HAVING COUNT(*) = (SELECT COUNT(*) FROM (SELECT DISTINCT userId FROM MyTable) AS UserCount)
ORDER BY totalBought DESC
) AS MostBought
ORDER BY totalBought
答案 1 :(得分:0)
我的理解是,您想要显示任何用户3次或更多次购买的itemNames ....?
SELECT a.itemName FROM
(SELECT
itemName AS itemName,
sum(action) AS action
FROM
(SELECT
a.itemName as itemName,
CASE
WHEN (action = 'bought')
THEN (1)
ELSE (0)
END AS action
FROM yourTableName) AS a
GROUP BY
itemName) AS a
where action > 2;
我尚未对此进行测试......
如果这不是你的解决方案,请告诉我,以便我可以探索其他选择..
答案 2 :(得分:0)
请尝试以下查询,列出所有用户购买的商品名称,并列在第3位
{{1}}