Jump to content

Need help with SQL statement.


aic007

Recommended Posts

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...