Page 1 of 1

getting the correct values

Posted: Thu May 26, 2005 12:31 am
by niceguy_reddy
I store some values in postgresql in varchar format. The values that I store in the varchar data type are either integer or numeric. I store 6 values for each user. I want to take only 4 greatest values and find their average. For example if store the values 13, 14, 6, 13, 13, 17 and I use this query to get the 4 greatest values,

select value from master where name='$name' desc limit 4;

it returns 6,17,14,13. I have to get 17,14,13,13. How can i do this, and if i store a numeric value like 13.5 the value 17,14,13.5,13 should be fetched.

try this....

Posted: Thu May 26, 2005 3:44 am
by rochakchauhan
select value from master where name='$name' desc ORDER BY value limit 4;

Posted: Thu May 26, 2005 5:51 am
by niceguy_reddy
if i give order by then it returns 8,13,14,17 when the values in the database are 13,8,14,12,13,17.

Posted: Thu May 26, 2005 6:15 am
by Bennettman
Last post had incorrect ordering syntax:

Code: Select all

SELECT value FROM master WHERE name='$name' ORDER BY value DESC LIMIT 4;