I have this SQL code that helps to determine the position of a specific entry based on its score compared to other entries.
select (select count(*)
from entries e2
where e1.score < e2.score) + 1 as pos
from entries e1
where e1.ID = 36;
Translating this into Sequelize.js has been challenging for me. I currently have a solution that involves 2 queries, but I'm striving to achieve this in just one query.
this.dal.Entries.getPositionInList = (ID: number) => {
return this.db.Entries.findOne({
where: {
ID: ID
}
}).then((entry: any) => {
return this.db.Entries.findAndCountAll({
where: {
score: {
$gt: entry.score
}
}
});
});
};