Solved

MS SQL order by

Posted on 2011-03-10
3
367 Views
Last Modified: 2012-05-11
Is it possible to do a MS SQL query where the order by is the sum of two fields?

If I have two numeric fields in the table and want the query to sort by the sum of those fields.
0
Comment
Question by:cdjohn31
3 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35093427
Yes, you can. The sum of two fields is, as far as SQL is concerned, just a field. It is calculated from 2 others, but it's computed as if it's a field like any other.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 35093428
Yes, you can do something like:
SELECT Value1, Value2, [Stuff] FROM SampleTable ORDER BY Value1+Value2

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35093482
ye you can like this
select col1,col2,(col1 +col2) as sumofcol from tablename
order by (col1 +col2)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now