I’d like to make a linear score formula that I can use in a view. I’m having trouble working out the logic though.
Data:
*UserID (int)| *WidgetID (int)| Rank (int) | IsPreferred (bit)
1 | 1 | 1 | 1
1 | 2 | 2 | 1
1 | 3 | 3 | 1
1 | 4 | 1 | 0
1 | 5 | 2 | 0
2 | 1 | 1 | 1
User #1 loves Widget #1 the most and hates Widget #4 the most. User #2 just loves Widget #1 (the most).
I would like the my view to look something like this:
*UserID (int)| *WidgetID (int)| Rank (int) | IsPreferred (bit) | Score (int)
1 | 1 | 1 | 1 | 3
1 | 2 | 2 | 1 | 2
1 | 3 | 3 | 1 | 1
1 | 4 | 1 | 0 | -2
1 | 5 | 2 | 0 | -1
2 | 1 | 1 | 1 | 1
The Rank always (for now) has an absolute between 1-5 (inclusive). UserID and WidgetID are both primary keys and the application forces consecutive unique ranks so I don’t have to worry about conflicting scores. I thought of doing something like this:
Select *, Case When IsPreferred = 1 Then (5 / Rank) Else -(5 / Rank) End As Score...
But that’s exponential and could cause problems if we allow for more then 5 rankings in the future…
I’m hoping I don’t have to do this in my application, cause that’s not very l33t. I’m thinking PARTITION or RANK may be able to help me out but I haven’t had any success yet.
Anybody got any ideas? Thanks
UPDATE see my answer below
This works, but I’m open to other ideas: