Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weighted values...

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Just wondering...does anyone know of a good way to execute a SQL query, with the values weighted. At the moment I am using RAND(), but I'm trying to get it so that I can add weights to the links, to get higher showing of some adverts than others.

i.e

Link 1 - weight 1
Link 2 - weight 4
Link 3 - weight 1
Link 4 - weight 2
Link 5 - weight 1

So link 2 would be shown 4 times more than link 2 (approx), and link 4 would be shown 2 times more than link 5, but 1/2 the amount of link 2.

Anyone got any suggestions?

Cheers :)

Andy
 
You could:

Get a random number from 1 to total of all weights,
Loop through each row adding the weights until you go past your number and take that result.

It's not a nice or fast way of doing it, but it's a way of doing it --BB
 
Here's a restatement with example.

So you assign 5 records 5 weights.

1 => 1
2 => 1
3 => 1
4 => 2
5 => 5
Sum= 10

Generate a random number from 0,9 (or 1-10 and replace greater than with equal to)

Now, loop through the rows summing their weights, when your sum is greater than the random number, grab that line.

Random # => Line showed
0=> 1
1=> 2
2=> 3
3=> 4
4=> 4
5=> 5
6=> 5
7=> 5
8=> 5
9=> 5


-Rob
 
Ah...I get you now :)

Thanks for the info...I'll give that a go with some code.

Cheers again.

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top