Jump to content

Help building a query


thenajsays

Recommended Posts

I have a number of tables in the database that i need to query at once and pull multiple results... the tables are as follows:

clients, including columns such as id, name, consultantId and fee

users, including an id with will correspond with clients.consultantId, name

payments, including the columns, paydate, amount, clientid which corresponds to clients.id, a boolean field indicating whether the scheduled payment was fulfilled.

 

What I need is a query that will look in the database at the clients, compare the sum of all fulfilled payments associated with that client and if the sum equals the fee associated with that client. if it does, i need to look up the date of the last payment and see when the following friday from that date is and pull the consultant data associated with that client from the users database

Link to comment
Share on other sites

i dont really have that either... im a bit stumped... i suppose it would look something like this:

SELECT clients.id, clients.name, clients.amount, clients.consultantId, users.id as uid, users.name, payments.paydate, SUM(payments.amount) as paysum, payments.payed FROM clients, users, payments WHERE payments.payed = 1 AND clients.amount = paysum AND  clients.consultantId = users.id

 

but again... not exactly what im looking for

Link to comment
Share on other sites

ok, i also tried this:

SELECT clients.id, clients.name, clients.amount, clients.consultantId, users.id as uid, users.name, payments.paydate, SUM(payments.amount) as paysum, payments.payed FROM clients, users, payments WHERE payments.payed = 1 AND clients.amount = paysum AND clients.consultantId = users.id GROUP BY payments.clientid ORDER BY payments.paydate DESC

didnt work... said invalid use of the GROUP BY function

Link to comment
Share on other sites

From what I can work out you are looking for something like this:

SELECT clients.name, clients.amount, users.name, MAX(payments.paydate) as lastPay, SUM(payments.amount) as paysum
FROM users 
LEFT JOIN clients ON
  (clients.consultantid = users.id)
LEFT JOIN payments ON
  (clients.id = payments.clientid)
WHERE payments.payed = 1
GROUP BY clients.id
ORDER BY lastPay

You will probably need to tweek the joins and fields selected at best but it should be enough to get you going.

Link to comment
Share on other sites

ok, this seems like a step in the right direction... here is the actual query i used (note that some fields are different as i simplified things previously when trying to explain:

$query = "SELECT clients.firstname as clfn, clients.lastname as clln, clients.fee, clients.type, CONCAT(users.firstname, ' ', users.lastname) as conname, MAX(payments.pdate) as findate, SUM(payments.amount) as paysum, leadsource.source

FROM users

LEFT JOIN clients ON

  (clients.consultant = users.id)

LEFT JOIN payments ON

  (clients.id = payments.clientid)

LEFT JOIN leadsource ON

  (clients.leadid = leadsource.id)

WHERE payments.cashed = 1 AND SUM(payments.amount) = clients.fee AND MONTH(payments.pdate) = $month AND YEAR(payments.pdate) = $year

GROUP BY clients.id

ORDER BY findate";

when i added in the clause "SUM(payments.amount) = clients.fee" it spat out an invalid use of the group by function... but without it, it pulls anyone with a payment made in that month

Link to comment
Share on other sites

Hi

 

Looks generally OK but would expect your GROUP BY field to be one you have selected (although really you should have all the non aggregate fields in the GROUP BY clause - some flavours of SQL insist on it).

 

Biggy I suspect is the missing spaces around the GROUP BY clause

 

All the best

 

Keith

Link to comment
Share on other sites

  • 2 weeks later...

ok, it seems it's not working all the way... in testing it worked fine, but put into production it failed miserably... here is my php code to build the query:

$query = "SELECT clients.firstname as clfn, clients.lastname as clln, clients.fee, clients.type, CONCAT(users.firstname, ' ', users.lastname) as conname,

DATE_ADD(CURRENT_DATE,INTERVAL mod(6-DAYOFWEEK(MAX(payments.pdate))+6,7) DAY)as findate, SUM(payments.amount) as paysum, leadsource.source

FROM users

LEFT JOIN clients ON

  (clients.consultant = users.id)

LEFT JOIN payments ON

  (clients.id = payments.clientid)

LEFT JOIN leadsource ON

  (clients.leadid = leadsource.id)

WHERE payments.cashed = 1 AND clients.type != 'Loan Modification'";

if(isset($_GET['consultant'])&&$_GET['consultant']<>'') $query .= " AND clients.consultant = ".$_GET['consultant'];

$query .= " GROUP BY clients.id";

if(!isset($_GET['from'])&&!isset($_GET['to'])&&!isset($_GET['exact'])) $query .= " HAVING SUM(payments.amount) = clients.fee AND MONTH(DATE_ADD(CURRENT_DATE,INTERVAL mod(6-DAYOFWEEK(MAX(payments.pdate))+6,7) DAY)) = $month AND YEAR(DATE_ADD(CURRENT_DATE,INTERVAL mod(6-DAYOFWEEK(MAX(payments.pdate))+6,7) DAY)) = $year";

else {

if(isset($_GET['from'])&&isset($_GET['to'])){

$from = explode("/",$_GET['from']);

$from = $from[2].'-'.$from[0].'-'.$from[1];

$to = explode("/",$_GET['to']);

$to = $to[2].'-'.$to[0].'-'.$to[1];

$query .= " HAVING SUM(payments.amount) = clients.fee AND DATE_ADD(CURRENT_DATE,INTERVAL mod(6-DAYOFWEEK(MAX(payments.pdate))+6,7) DAY) >= '$from' AND DATE_ADD(CURRENT_DATE,INTERVAL mod(6-DAYOFWEEK(MAX(payments.pdate))+6,7) DAY) <= '$to'";

}

if(isset($_GET['exact'])){

$exact = explode("/",$_GET['exact']);

$exact = $exact[2].'-'.$exact[0].'-'.$exact[1];

$query .= " HAVING SUM(payments.amount) = clients.fee AND DATE_ADD(CURRENT_DATE,INTERVAL mod(6-DAYOFWEEK(MAX(payments.pdate))+6,7) DAY) = '$exact'";

}

}

$query .= " ORDER BY findate";

 

for some reason, this is taking everything in the database and assuming that the month in the date is THIS month... and i cant for the life of me fathom why :confused::shrug:

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.