Jump to content

DISCTINCT SUM()


kathleennewbie

Recommended Posts

Having a really hard time with this...

 

 
$get_total_income = mysql_query("SELECT SUM(tAmount) FROM transactions WHERE tStatus = 1 AND tUserType = 1", $db2) or die("Query error: ". mysql_error());
               $total_income = number_format(mysql_result($get_total_income,0),2);

 

The problem is that it is summing all transactions, where say a member will be on auto-bill each month for 29.95 and we only want to show what the total monthly residual is, not the total overall income combined of all months... so we need to select just the latest transaction from each member

 

EXAMPLE:

tUserIDtAmountunixtime

119.95

214.95

329.95

139.95

219.95

529.95

129.95

 

USER ID 1 has 3 payment records (date filed is also in the table) We need to show the latest payment (so in between two dates) and add all the paymetns together.

 

So latest payment for user ID 1 is 19.95 (current monthly payment) plus (+) user ID 2 is 14.95 (current monthly payment) + userID 3 is 29.95 (current monthly payment) + user ID 5 is 29.95 (current monthly payment).

 

So we want to select the last inserted record of each user and sum them all together, skipping user duplicates or previous entered records.

 

select

 

Link to comment
Share on other sites

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.