0
申请行的子集
我有如下表:条件列在MySQL
row_number itemID score
1 45 99
2 45 47
1 46 98
2 46 20
1 47 98
2 47 20
… … …
这是下面的查询的输出:
SET @num := 0, @itemID_grouping := '';
SELECT row_number,
itemID,
score
FROM (SELECT itemID,
score,
@num := IF(@itemID_grouping = itemID, @num + 1, 1) AS row_number,
@itemID_grouping := itemID AS temp
FROM my_table
ORDER BY itemID,
score DESC) AS x
WHERE x.row_number <= 2;
从下面的原始表:
my_table:
itemID score
46 99
… …
因此,对于每个itemID
,我有一组两个值(两个最高分)。
我想另一列包含'y'或1,如果,对于每组,具有row_number=1
的数字大于某个阈值并且row_number=2
的数字低于某个阈值。有任何想法吗?