Skip to content
Advertisement

Delete rows to keep only top values in a sql table

I am trying to have some product recommendations in my website. I have this table that i have generated but need to keep a limited amount of entries.

The table consists of these columns (filling with some random values):

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

How can i trucate top 10 SCORE values for each PRODUCT ID in this table?

Advertisement

Answer

Note — you did not say what platform, the following will work in SQL Server, Oracle, DB2 and others

The following statement will give you the top 10 ten rows

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

So then delete everything else

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
)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement