Solved

Query PHP/MYSQL question

Posted on 2011-03-16
10
238 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
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:
shinuq 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
 
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 count occurrences of each item in an array.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now