Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sorting by Addition of fields

Author  Topic 

Erad
Starting Member

4 Posts

Posted - 2009-07-21 : 02:31:12
Hey all,

I have a table that has many fields, but 4 important 'int' ones. For simplicity's sake, we'll call them 1, 2, 3, and 4. I need a query that will take 10 rows, in order of descending sums of these 4 fields. For the result of the query, all I need is a field called 'name' and the actual sum of 1, 2, 3, and 4 for that row.

Erad
Starting Member

4 Posts

Posted - 2009-07-21 : 02:33:51
I forgot to mention that my first attempt was:

SELECT *
FROM `table`
ORDER BY SUM(1+2+3+4)
LIMIT 10

but this didn't work
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-07-21 : 02:38:22
are you looking for this....

SELECT TOP 10*
FROM `table`
ORDER BY (1+2+3+4) desc


Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

Erad
Starting Member

4 Posts

Posted - 2009-07-21 : 03:00:03
quote:
Originally posted by Mangal Pardeshi

are you looking for this....

SELECT TOP 10*
FROM `table`
ORDER BY (1+2+3+4) desc


Mangal Pardeshi
http://mangalpardeshi.blogspot.com



#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10* FROM `table` ORDER BY (1+2+3+4) desc LIMIT 0, 30' at line 1

Close...
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-07-21 : 03:05:56
Ahh... These are the forums for SQL Server.
You need to ask MySQL related questions on MySQL forums.

Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

Erad
Starting Member

4 Posts

Posted - 2009-07-21 : 03:10:16
My apologies, thanks for the help anyways
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-07-21 : 03:12:22
http://forums.mysql.com/

Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page
   

- Advertisement -