我正在尝试在我的网站上提供一些产品推荐。我有我生成的这张表,但需要保留有限的条目。
该表由以下几列组成(填充了一些随机值):
ID | Product ID | Related Product ID | Score
1 | 10 | 111 | 0.1
2 | 10 | 123 | 0.4
3 | 10 | 124 | 0.5
4 | 10 | 125 | 0.4
5 | 10 | 126 | 0.2
6 | 10 | 127 | 0.4
7 | 11 | 111 | 0.1
8 | 12 | 123 | 0.4
如何为该表中的每个PRODUCT ID列出前10个SCORE值?
答案 0 :(得分:0)
注意-您没有说什么平台,以下将在SQL Server,Oracle,DB2和其他平台上工作
以下语句将为您提供前十行十行
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY PRODUCT_ID ORDER BY SCORE DESC) AS RN
FROM SOME_TABLENAME_YOU_DID_NOT_SAY
) X
WHERE X.RN < 11
然后删除所有其他内容
DELETE FROM SOME_TABLENAME_YOU_DID_NOT_SAY
WHERE ID NOT IN
(
SELECT ID
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY PRODUCT_ID ORDER BY SCORE DESC) AS RN
FROM SOME_TABLENAME_YOU_DID_NOT_SAY
) X
WHERE X.RN < 11
)