Solved

Query PHP/MYSQL question

Posted on 2011-03-16
10
269 Views
Last Modified: 2012-05-11
Hello Experts,

I was wondering if it was possible to "add" together all the numbers contained in a table based on the "company name" I ask this because the company have more than one phone per a business and need to be added up and sorted dynamicly. from highest to lowest.

An example of my table is attached

So what im trying to do is get the following

Company                TOTAL SPEND
ANNA Norris           £178
PETER jones            £100

As you can see it adds up all fields where Anna norris is the company name. as totals it.. so this would ADD both mobile number spends together. Because two people have phones in One company.

It then would need be orderd from highest to lowest?

Is This possible with a query??

if not is there any other ways you can think of??

All help is appricated.




Untitled.jpg
0
Comment
Question by:NeoAshura
[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
10 Comments
 
LVL 3

Expert Comment

by:fabzster187
ID: 35146767
ORDER BY DESC

Should give u this
0
 
LVL 3

Expert Comment

by:fabzster187
ID: 35146773
sorry ORDER BY TOTAL SPEND DESC
0
 
LVL 17

Accepted Solution

by:
Shinesh Premrajan earned 250 total points
ID: 35146827
select sum(jan) as total from company group by company
union
select sum(feb) as total from company group by company
union
select sum(mar) as total from company group by company order by total desc

Try with this query, see if its work then you can use the same for the rest of the months
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 4

Assisted Solution

by:qasim_md
qasim_md earned 250 total points
ID: 35146866
This will give you what you want::::

select sum(total_spend),company from your_table group by company order by 1 desc

did this help ???
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35147145
Ill try these when I get back to computer just in a meeting atm. Thanks will keep u updated
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35147162
Btw total spend is NOT a table it is the total needing to be displayed by PHP once the motnhly totals have been added
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35148154
ok guys im back at computer, i tried

select sum(total_spend),company from your_table group by company order by 1 desc

but get the following error:

Unknown column 'total_spend' in 'field list'

Thats because total spend needs to be the value which is added together from the months....



I Also tried:

"select sum(Jan09) as total from Bill2009 group by company
union
select sum(Feb09) as total from Bill2009 group by company
union
select sum(Mar09) as total from Bill2009 group by company order by total desc

This returned me with a table but with blank fields...

Anymore Ideas?????
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35148169
P.S what ive got is the table as shown attached in my first opening question.

What i need from that table is the output of

Company     TOTAL SPEND
Just LTD        £200
Deck LTD       £150

THERE IS NO TOTAL SPEND COLUMN IN MY TABLE THIS MUST BE CREATE VIA ADDING UP THE JAN FEB ETC.. AND THEN SHOWING TOTAL SPEND AS THE OUTPUT USING PHP.

Is this possible??
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35155069
I solved my own problem. However some experts helped me get that answer so points will be awarded accordingly.
0
 
LVL 6

Author Closing Comment

by:NeoAshura
ID: 35155076
was not full answer, created the answer myself.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Formating field in mysql Advance formatting 1 41
What is define("__APPROOT__", __DIR__); 6 32
Echo'd values in dropdowns 6 30
JSON decode 5 20
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 is …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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