PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 5th, 2004, 07:49 AM
mvallabh mvallabh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 68 mvallabh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 10 m 28 sec
Reputation Power: 6
Angry Ranking Variable

Hi

I would like to know if it's possible to make a ranking variable in a query which numbers the rows from 1, 2, 3, 4, 5.....n consecutively. I need to rank the results I receive for a particular resultset, which are displayed in descending order.

tx in advance

Reply With Quote
  #2  
Old January 5th, 2004, 12:16 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Psycho Canadian
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jan 2001
Location: Canada
Posts: 4,795 Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Weeks 23 h 51 m 32 sec
Reputation Power: 437
Do you mean just displaying 1 2 3... beside each record? If so this is normally done programmatically not in a query though I think it can be done I just haven't done it before. Can you clear up what you're trying to get.
__________________
Miscellaneous Software
Viper_SB
Developershed E-Support


Anyone else play chess?
Challenge me

Last edited by Viper_SB : January 5th, 2004 at 07:04 PM.

Reply With Quote
  #3  
Old January 5th, 2004, 12:59 PM
metaBarf metaBarf is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 373 metaBarf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 49 m 6 sec
Reputation Power: 6
you could possibly define a SEQUENCE that would keep track of your last ranking.

Or do you want to do it all at once, just make the "ranked" table/view and be done with it?

I had some inkling from somewhere that primary keys create a sequence automagically, and thus when you insert this field is generated??? sorry I don't have time to try it today...

or, you can fill a table with the results of a query with CREATE TABLE AS and just select from a table that already has sequential numbers in it, or write a procedure that would use the sequence from before as its starting point, and run a trigger on INSERT into the table that you were originally ranking. It depends on when you are assigning the ranking in your overall problem.

Reply With Quote
  #4  
Old January 5th, 2004, 01:14 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,246 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 17 h 3 m 28 sec
Reputation Power: 1055
i've done it with pure sql before, it is a very inefficient query but it can be done

still, you should consider doing it in your application program as Viper_SB suggested
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
  #5  
Old January 6th, 2004, 01:46 AM
mvallabh mvallabh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 68 mvallabh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 10 m 28 sec
Reputation Power: 6
Ranking variable reply

Hi

Thanx for the suggestions!

I know I cannot use the primary key cos the order in which I get the resultset will constantly change. And yes I am basically numbering the rows in the order that they are retrieved.
As the data will be ordered from highest to lowest premium amounts.

I would like to know if there is any documentation available in order to do this programatically, and whether it would negatively affect the performance of the query?

Tx

Reply With Quote
  #6  
Old January 6th, 2004, 02:09 AM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Psycho Canadian
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jan 2001
Location: Canada
Posts: 4,795 Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Weeks 23 h 51 m 32 sec
Reputation Power: 437
Doing it programatically shouldn't affect the performance and in my opinon I think would help it. Which language are you coding in?

Reply With Quote
  #7  
Old January 6th, 2004, 03:08 AM
mvallabh mvallabh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 68 mvallabh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 10 m 28 sec
Reputation Power: 6
I am using JAVA and XML to display the reports. And using postgres psql to write functions

Reply With Quote
  #8  
Old January 6th, 2004, 11:23 AM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Psycho Canadian
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jan 2001
Location: Canada
Posts: 4,795 Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Weeks 23 h 51 m 32 sec
Reputation Power: 437
I don't use Java but I'd imagen it's something similar to (syntax maybe off):

Code:
i = 1;
while (YourPSQLFunctionHere) {
     print i;
     print therecord;
     ++i;
}


or if you use a for loop it's even easier

Code:
for(i=1; YourPSQLFunctionHere; i++) {
    print i;
    print therecord;
}



something like that would be the easiest way to go, you might want to ask in the java forum for the correct syntax

Reply With Quote
  #9  
Old January 6th, 2004, 11:37 AM
mvallabh mvallabh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 68 mvallabh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 10 m 28 sec
Reputation Power: 6
tx viper, will try it out...and let you know

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Ranking Variable


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT