Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I change this select statement?

Posted on 2011-09-22
5
Medium Priority
?
234 Views
Last Modified: 2012-05-12
Here's my query:

$sour = "SELECT * FROM revenue_tally where student_id = '$student_id' AND client_type='Top 7 Prospect' GROUP by company_name";

When I include the "GROUP by company_name" dynamic, should I have more than one row with the same company name, the row that is being displayed isn't always the one that has income attached to it.

In other words, here's my table:

Gust's Seashells - $4240
Gust's Seashells - $0.00

The entry that's being displayed is the one with zero dollar amount. So it will show up on my page as:

Gust's Seashells - $0.00

I need to display the entry that has revenue attached to it. How can I write my query so when the grouping occurs, it displays the row with income as opposed to the row with no income?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Expert Comment

by:themrrobert
ID: 36581705
change to:

$sour = "SELECT * FROM revenue_tally where student_id = '$student_id' AND client_type='Top 7 Prospect' GROUP by company_name ORDER BY `VALUE_KEY` SORT DESC";

replace VALUE_KEY with the name of the income amount, the group by company name only groups like companies together, it has nothing to do with the dollar amount. Also, Mysql is usually smart enough to order by dollar amounts even if stored as text, so you should be good
0
 
LVL 7

Expert Comment

by:amigura
ID: 36581811
comp 1 200.00
comp 2 30.00
comp 2 20.00
comp 1 0.00
comp 3 10.00

comp 1 200
comp 2 50
comp 3 10

if thats what you want

$sour = "SELECT company, SUM(revenue or whatever) as revenue_total  FROM revenue_tally as revenue_total where student_id = '$student_id' AND client_type='Top 7 Prospect' GROUP by company_name";

Open in new window

0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36581869
you could always add a where income > 0 to the end of the query.
0
 

Author Comment

by:brucegust
ID: 36581910
We're close!

Here's what I did:

$sour = "SELECT *, sum(actual_revenue) as revenue_total FROM revenue_tally where student_id = '$student_id' AND client_type='Top 7 Prospect' GROUP by company_name order by revenue_total DESC";

I'm still not there,however. Here's what I get as a result:


Louisville Oral Surgery  $17,600  
Evangel World Prayer Center  $10,000  
In Every Language  $0  
AVC Security Systems  $3,500

You'll notice that "In Every Language" shows up as 0$. That's because one of the "In Every Language" entries is $4240, while the other is $0. How do I get the $4240 value to show up?
0
 
LVL 7

Accepted Solution

by:
amigura earned 2000 total points
ID: 36581992
but does "$4240 In Every Language" fall into the category of student_id = '$student_id' AND client_type='Top 7 Prospect' and also not doing so

sum(actual_revenue) as revenue_total

is

$revenue_total=$row["revenue_totalt"];
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

722 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