aic007 Posted May 7, 2007 Share Posted May 7, 2007 Hi I am using a SQL statement to get the ten most expensive computers, but I don't get the 10 most expensive computers. Instead I get a mix of cheap and expensive computers. This is my SQL statementSET ROWCOUNT 10SELECT navn, prisFrom ComputersORDER BY pris DESCDoes anybody have any idea of what I am doing wrong ? Link to comment Share on other sites More sharing options...
jesh Posted May 7, 2007 Share Posted May 7, 2007 It could be that your price column is storing text instead of numbers. A descending sort of text might result in something like the following: Name Pricecomputer1 999.00computer2 99.00computer3 8888.00 Check to make sure that your price column is int (if you are storing integers) or something like float, double, or decimal (if you are storing floats). Link to comment Share on other sites More sharing options...
aic007 Posted May 7, 2007 Author Share Posted May 7, 2007 Thanx a lot mate. My pris (price) field was set to nvarchar. I used int, as you said, and the problem was solved Link to comment Share on other sites More sharing options...
aic007 Posted May 8, 2007 Author Share Posted May 8, 2007 Hi again. I was wondering one thing. I still want to have the ten most expensive computers, but I do not want to sort them - group buy DESC price. I want them to be random. How can I do that ? Link to comment Share on other sites More sharing options...
aspnetguy Posted May 8, 2007 Share Posted May 8, 2007 I don't understand. To get the 10 most expensive you have to order by price desc. You could return every record and sort it in your code but that would be a lot slower and unnecesary code. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 8, 2007 Share Posted May 8, 2007 If you want the 10 most expensive computers in a random order, the easiest way would be to get the 10 most expensive computers, save them into some sort of structure like an array, and randomize the array. You could do it with SQL, but it would be a lot more complex and would consist of something like selecting the top 10 items into a temporary table or a view with an integer column that holds a random number, and then selecting from that table and ordering by the random number. Link to comment Share on other sites More sharing options...
Yahweh Posted May 9, 2007 Share Posted May 9, 2007 I was wondering one thing. I still want to have the ten most expensive computers, but I do not want to sort them - group buy DESC price. I want them to be random. How can I do that ?I'm not sure why you'd want to sort them like that, but you can do that with a simple subquery. Assuming you have a uniqueID called "ID", you can do this:SELECT navn, prisFROM ComputersWHERE ID in (SELECT TOP 10 ID from Computers ORDER BY pris DESC)ORDER BY newID() Link to comment Share on other sites More sharing options...
aic007 Posted May 10, 2007 Author Share Posted May 10, 2007 Hi.Thanx alot for helping, everybody. Nice code, Yahweh, I'm using it now. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.