Query PHP/MYSQL question

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
LVL 6
NeoAshuraAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Shinesh PremrajanConnect With a Mentor Engineering ManagerCommented:
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
 
fabzster187Commented:
ORDER BY DESC

Should give u this
0
 
fabzster187Commented:
sorry ORDER BY TOTAL SPEND DESC
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
qasim_mdConnect With a Mentor Commented:
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
 
NeoAshuraAuthor Commented:
Ill try these when I get back to computer just in a meeting atm. Thanks will keep u updated
0
 
NeoAshuraAuthor Commented:
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
 
NeoAshuraAuthor Commented:
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
 
NeoAshuraAuthor Commented:
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
 
NeoAshuraAuthor Commented:
I solved my own problem. However some experts helped me get that answer so points will be awarded accordingly.
0
 
NeoAshuraAuthor Commented:
was not full answer, created the answer myself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.