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

x
?
Solved

How to make a sum per month of payments using php and mysql?

Posted on 2012-04-04
3
Medium Priority
?
524 Views
Last Modified: 2012-07-16
HI

I need to perform a query from php that requests to a mysql database the sum of payments of a customer per month, I would like to know how to construct this query from php based on this table:

PAYMENTS
IdPayment, IdCustomer, PaymentDate, Amount

The query must show something like a balance since the first payment to the last one:

JANUARY 2012 (this date must be since the first payment of the customer)
IdCustomer, Total Amount Paid

FEBRUARY 2012
IdCustomer, Total Amount Paid
...

and so on.


Regards
0
Comment
Question by:dimensionav
[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
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
johanntagle earned 1000 total points
ID: 37808461
I'm not a PHP developer but the SQL should be something like:

select date_format(PaymentDate, '%M %Y') payment_month, IdCustomer, sum(Amount)
from PAYMENTS
group by year(PaymentDate), month(PaymentDate), IdCustomer
order by year(PaymentDate), month(PaymentDate), IdCustomer

Then in your PHP code you need to track the payment_month column of the result set - if it changes it means you have a new month and so you should print whatever headers you want.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37809726
There was a similar question in the Oracle zone recently. With 'balance' the Asker there didn't mean the sum per period but the total from the start until that point in time. I submitted a solution with a subquery but there was a better solution in PL/SQL using 'partition', not sure if anything like that is available in MySQL so I'll submit my 'low-tech' solution here as well:

SELECT p1.IdCustomer, date_format(p1.PaymentDate, '%M %Y') payment_month, (SELECT SUM(Amount) FROM PAYMENTS p2 WHERE p2.IdCustomer = p1.IdCustomer AND p2.PaymentDate <= LAST_DAY(p1.PaymentDate)) balance
FROM PAYMENTS p1
GROUP BY p1.IdCustomer, payment_month
ORDER BY p1.IdCustomer, YEAR(p1.PaymentDate), MONTH(p1.PaymentDate)

Open in new window

0
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 1000 total points
ID: 37809768
For the php part, there's a lot of examples to be found on the net, like this one: http://www.anyexample.com/programming/php/php_mysql_example__display_table_as_html.xml 

Or do you need specific help, in that case please post the (relevant) code?
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
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…
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 …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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