Jump to content

Getting total for each client between date range.


Recommended Posts

Setup:

Mysql table 1: acct#, client name, status(active/not active), Starting balance, ending balance

Mysql table 2: acct#, invoice amount, invoice date, total

 

 

how do i select all customers that are Active, and the starting balance != ending balance and then get the total for all invoices for that customer where the invoice date is between 2010-01-01 and 2010-03-01

 

Basically my out put needs to look like this.

 

Customer name, Total of all invoices for customer

john doe,  $10,564.21 

Susy Scott, $158.02

Bang Head, $837,294.85

 

Customer can have multiple invoices within the given date range. But I only need the total for all the invoices for each customer added up and echo'ed.

 

Quickness is also a factor on the query and echo.

 

Thank you for your valuable time.

 

 

Try this: un-tested.

$sql = "SELECT 
             a.`client name`,
             SUM(b.total) as total 
           FROM 
              table1 AS a, 
              table2 AS b 
           WHERE 
              a.acct# = b.acct# 
                   AND 
                     a.status = 'active'
                   AND 
                     a.`Starting balance` != a.`ending balance` 
                   AND 
                     b.`invoice date` 
                           BETWEEN 
                              '2010-01-01' 
                           AND 
                              '2010-03-01'";

This did not work.  Also let me clearify that the amount field in table 2 is a varchar and not a dec as it looks like this $33,234.39;

 

I know a way I can get all the records I need but then how would i loop through them and calculate the total for each customer..

 

example:

customer 1, acct #1, $13.02

customer 1, acct #1, $345.02

customer 1, acct #1, $1,353.02

customer2 , acct #2, $2.25

customer 2, acct #2, $11.25

customer2 , acct #2, $11.24

customer2 , acct #2, $12.50

customer2 , acct #2, $14.60

customer3 , acct #3, $4,521.25

 

This would be all records but i just want sum for

customer1 -> total of all 3 records

customer2 -> total of all 5 records

customer3 -> total of the 1 record

well needless to say I took over a dev spot of someone who thought they were SUPER but with that still being said, I can't just go change the db right now but I am in the process of changing a lot of projects and db structures 1 at a time..

 

Right now I just have to make do. I can get all the records I need with a join, but have to figure how to split my results into groups by customer # and then cal the sum of that customer

Running of the same premise. Un-tested.  Assuming money is stored as XX.XX and not $XX.XX.

 

$sql = "SELECT 
             a.`client name`,
             b.total 
           FROM 
              table1 AS a, 
              table2 AS b 
           WHERE 
              a.acct# = b.acct# 
                   AND 
                     a.status = 'active'
                   AND 
                     a.`Starting balance` != a.`ending balance` 
                   AND 
                     b.`invoice date` 
                           BETWEEN 
                              '2010-01-01' 
                           AND 
                              '2010-03-01'";
$result = mysql_query($sql);
while($r = mysql_fetch_assoc($result)) {
$money[$r['client name']] += $r['total'];
}
if(is_array($money)) {
foreach($money as $name => $total) {
echo 'Customer ' . $name . ' owes a total of $' . $total . '<br />' . "\n";
}
}

 

If money is stored as $XX.XX

//replace
$money[$r['client name']] += $r['total'];
//with
$money[$r['client name']] += substr($r['total'],1);

Running of the same premise. Un-tested.  Assuming money is stored as XX.XX and not $XX.XX.

 

$sql = "SELECT 
             a.`client name`,
             b.total 
           FROM 
              table1 AS a, 
              table2 AS b 
           WHERE 
              a.acct# = b.acct# 
                   AND 
                     a.status = 'active'
                   AND 
                     a.`Starting balance` != a.`ending balance` 
                   AND 
                     b.`invoice date` 
                           BETWEEN 
                              '2010-01-01' 
                           AND 
                              '2010-03-01'";
$result = mysql_query($sql);
while($r = mysql_fetch_assoc($result)) {
$money[$r['client name']] += $r['total'];
}
if(is_array($money)) {
foreach($money as $name => $total) {
echo 'Customer ' . $name . ' owes a total of $' . $total . '<br />' . "\n";
}
}

 

If money is stored as $XX.XX

//replace
$money[$r['client name']] += $r['total'];
//with
$money[$r['client name']] += substr($r['total'],1);

 

Got me one step closer. my print_r($r) returns right

Array

(

    [FirstName] => SUSAN ANNETTE

    [amount] => $218.10

)

 

Array

(

    [FirstName] => SUSAN ANNETTE

    [amount] => $217.86

)

 

Array

(

    [FirstName] => SUSAN ANNETTE

    [amount] => $137.74

)

With many more records.

 

And I used the format for $xx.xx but it returns as follows

Customer SUSAN ANNETTE owes a total of $0

Customer MICHAEL MARK owes a total of $0

Customer PAMELA BROOKS owes a total of $0

 

Your being a great help than you.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.