We help IT Professionals succeed at work.
Get Started

order by column that is result of sum()

361 Views
Last Modified: 2013-12-12
I have a query which joins results from two tables one regarding members informaiton and another with monetary values attributed to them.

One of the columns uses a result from sum(), summing a dollar amount. I want to order the results of the query by that particular column DESC so that the larger summed amount is at the top of the list.

Here is my current query:

$selrecord_yma = "2010";
if (isset($_POST['selrecord'])) {
  $selrecord_yma = (get_magic_quotes_gpc()) ? $_POST['selrecord'] : addslashes($_POST['selrecord']);
}
mysql_select_db($database_bninsteins_mysql, $bninsteins_mysql);
$query_yma = sprintf("SELECT s.member_id, s.year, SUM(s.amount) AS 'memberTotal', b.status, b.first_name, b.last_name FROM SMTM AS s JOIN members AS b ON s.member_id = b.member_id WHERE s.year = %s GROUP BY b.status, b.last_name, b.first_name ORDER BY 'memberTotal' DESC", GetSQLValueString($selrecord_yma, "int"));
$yma = mysql_query($query_yma, $bninsteins_mysql) or die(mysql_error());
$row_yma = mysql_fetch_assoc($yma);
$totalRows_yma = mysql_num_rows($yma);


I have also tried ordering by s.amount with no success. It comes up with and order, but not numerical.

The sum() works properly, but the order by doesn't result in a greater to lesser amounts list of rows.

Thanks for your patience as I am not sure I am even asking this question correctly. I appreaciate your help.
Comment
Watch Question
CERTIFIED EXPERT
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
This problem has been solved!
Unlock 2 Answers and 3 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE