473,508 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with query involving automatically decaying values

1 New Member
Hi,

I'm trying to put together a query that will order entries in a table by the rating values which decay over time. I'm not too well versed in the intricacies of Postgres, or complex queries, but I've cobbled the following together:

Expand|Select|Wrap|Line Numbers
  1. select * from entries order by (
  2.    select sum(-rating_value * exp(ln(0.5)/43200 * 
  3.                     extract(epoch from now() - rating_time))) 
  4.    from ratings where ratings.entry_id = entries.id)
  5.  
There are two tables - entries and ratings. The ratings table stores a few values such as the time of the rating, the user who contributed the rating, the initial value, the related entry id, etc. The -rating_value was just thrown in while I was testing and could just as easily be replaced by a DESC. The ln(0.5)/43200 causes the rating to lose half its current value every 12 hours.

It seems that as the tables grow, this query could start taking quite a bit of time (it's already quite a bit slower than my average query), so I had a few questions:

1.) Is there a much better way to do this, via JOINs or something else? I was unsuccessful in constructing a working JOIN.

2.) Is there any way to reduce the number of selects? This will run a select for each entry in the database to grab a rating value for ordering.

3.) Is there a better formula that would prevent the need for a SUM on every select everytime the query is run? It is important to retain the shown behavior so that an entry that receives 500 good ratings today will show up as "popular" for the moment, but if nobody ever votes on it again we don't have to wait until the 500 vote record is broken to see the more recent popular items. Basically, the items will slowly settle into their "overall" popular positions after enjoying the spotlight for a few days.

I appreciate any help. Thanks in advance!

-Dave
Apr 28 '07 #1
0 1450

Sign in to post your reply or Sign up for a free account.

Similar topics

0
482
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having...
9
4017
by: Edward S | last post by:
I budget for a Project in an Excel sheet as illustrated below. The months below are usually a 2 year period i.e. 24 months, though it could be over 24 months depending upon a Project. I then...
1
1669
by: Eskil | last post by:
Hi I have a form that supplies my query with information on two different variables. The form uses a lookup to display a list of 5 different customer types and 5 different types of...
2
5649
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
8
2215
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
6
4322
by: issac | last post by:
Hi folks Im trying to do a simple query involving the distinct keyword and an access 2000 db, but have been frittering with it for amost and hour and a half and I cant make it work. This is...
3
1867
by: KishenGajjar | last post by:
I'm trying to convert a query that runs in Query Analyser (SQL Server 2000) to work in MS Access. This will then help me modify the query further and use it to create reports and such. This query...
7
2003
by: Rnykster | last post by:
I know a little about Access and have made several single table databases. Been struggling for about a month to do a multiple table database with no success. Help! There are two tables. First...
16
3462
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.