Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

order by a division of two fields

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
what is the proper syntax to ORDER BY a division of two fields.

e.g.
select field1, field2 from table
order by ( i want it ordered by field1/field2)

i'm using mysql. thanks
 
May I recommend asking the question in the MySQL forum - forum436.

In some RDMS, you can simply write the query as follows. However, I do not know if MySQL allows any of this syntax.

Select field1, field2
From table
Order By (field1/field2)

Some RDMS require that an column in the Order By Clause be in the Select List.

Select field1, field2, field1/field2 As Result
From table
Order By (field1/field2)

Other RDMS allow ordering by the ordinal number of columns in the select list.

Select field1, field2, field1/field2 As Result
From table
Order By 3 Terry L. Broadbent - Salt Lake City, UT
Home of the 2002 Winter Olympics (Feb 8-24)
 
Sorry, I didn't clarify my question completely, how would i go about ordering by division of a sum and a count. Example below where the (ttl_1/ttl_3) section is:

SELECT SUM(field1) as ttl_1, SUM(field2) as ttl_2, field3, COUNT(field4) as ttl_3, (ttl_1/ttl_3)) as avg
FROM table
GROUP BY field3
ORDER BY avg

Does anyone know the proper syntax for this. Thanx Terry for above help.
 
Did you try any of the queries I provided? According to the MySQL manual some of them should work. The following text comes from the Online MySQL manual.

Columns selected for output may be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions.

Reference:
Note: Column position is the same as Ordinal Number that I referred to in the 3rd query example.

Once again, I encourage you to use the MySQL forum rather than the ANSI forum for MySQL questions. Terry L. Broadbent - Salt Lake City, UT
Home of the 2002 Winter Olympics (Feb 8-24)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top