Jump to content

Archived

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

nickn

Couple Questions, query related.

Recommended Posts

Here is the basic fields of a table.

 

field 1, field 2, field 3. Field 1 is varchar, 2 and 2 are int.

 

Select field1, sum(field2), sum(field3) GROUP BY field1 ORDER BY ??? sum(field2)???

 

I am trying to put the returned results in order from the sum of field 2

 

ie...

 

field1 field 2 field3

nick 20 2

matt 10 4

jason 5 1

 

 

Another Question. Can I order by 2 sums that have been divided.

 

 

ie...

 

SELECT field1, sum(field2), sum(field3) FROM TABLE GROUP BY field1 ORDER BY (field2 / field3) DESC

 

 

Again with the sum of 2 fields, then divided to get an avg between the 2 sums and ordered correctly by the avg.

 

ie...

 

field 1 field2 field3 AVG

nick 20 5 4

matt 15 5 3

jason 10 5 2

Share this post


Link to post
Share on other sites

You can order by any formula you wish

 

Either of these will work :

 

select col1, col2/col3

from table

order by col1/col2

or

 

select col1, col2/col3

from table

order by col2/col3

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.