Solved

How can I change this select statement?

Posted on 2011-09-22
5
226 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 500 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

626 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