943,668 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 598
  • MySQL RSS
Jul 5th, 2009
0

Need to get data for months no payment was made

Expand Post »
Hello,

I need to write a query for MySQL in vbscript that is just not coming to me. I have written it in vbscript with two SQL statements then did a date comparison in vbscript but it just grinds the server to a halt.

What I need to do is run thought a list of clients and determine the last month a full payment was made by the client then dump that info onto a webpage. So if a client has made a last full payment in January they will show up on the February list as a non-payment, but not on March’s or any other later month’s list as a non-payment. Likewise if the last full payment was made in February they need to only show up on the March’s list for owing a payment.

There are three tables in this query
1. Client (C_FName, C_LName, C_SIN(primary key))
2. Payment (P_Type, P_Amount, P_ID(primary key))
3. Payment_Board (B_Month, B_AmountPaid, B_ID(Foreign Key))

I have written some code that does the exact opposite of what I need but am wondering if it is close.
MySQL Syntax (Toggle Plain Text)
  1. strSQL = “ SELECT C_LName, C_FName, C_SIN, B_Month "
  2. strSQL = strSQL & " FROM Client, Payment_Board, Payment "
  3. strSQL = strSQL & " WHERE C_No =" & Store_Numb
  4. strSQL = strSQL & " AND C_Delete <>" & 1
  5. strSQL = strSQL & " AND B_Month BETWEEN '" & dtIniDate & "' AND '" & dtEndDate & "'"
  6. strSQL = strSQL & " AND B_SIN = C_SIN "
  7. strSQL = strSQL & " AND B_ID = P_ID "
  8. strSQL = strSQL & " AND P_Type =" & "'" & SetMonthlyDues & "'"
  9. strSQL = strSQL & " AND B_AmountPaid = P_Amount"
  10. strSQL = strSQL & " ORDER BY C_LName, C_FName, B_Month "
  11. %><!--#include file="../Scripts/DbExe.asp"--><%
  12. Do Until objRS.EOF
  13.  
MySQL Syntax (Toggle Plain Text)
  1. strSQL = “ SELECT C_LName, C_FName, C_SIN, B_Month "
  2. strSQL = strSQL & " FROM Client, Payment_Board, Payment "
  3. ‘C_No is the store id being compared to a variable identifying the store that is logged in
  4. strSQL = strSQL & " WHERE C_No =" & Store_Numb
  5. ‘C_Delete is to see if the client is still active
  6. strSQL = strSQL & " AND C_Delete <>" & 1
  7. ‘Checks to see if there is a record for that month, dtIniDate is first of month and dtEndDate is last day of month
  8. strSQL = strSQL & " AND B_Month BETWEEN '" & dtIniDate & "' AND '" & dtEndDate & "'"
  9. strSQL = strSQL & " AND B_SIN = C_SIN "
  10. strSQL = strSQL & " AND B_ID = P_ID "
  11. ‘Determines the type of payment made
  12. strSQL = strSQL & " AND P_Type =" & "'" & SetMonthlyPayment & "'"
  13. ‘Ensures a full payment has been made
  14. strSQL = strSQL & " AND B_AmountPaid = P_Amount"
  15. strSQL = strSQL & " ORDER BY C_LName, C_FName, B_Month "
  16. %><!--#include file="../Scripts/DbExe.asp"--><%
  17. Do Until objRS.EOF
  18.  

I was thinking of running this query once and creating a temp table or array then running the query again with the next month’s dates for dtIniDate and dtEndDate then comparing the values to see if the client is in the first table but not the second.

All I really need is for MySQL to go through each client’s records and if the last record found is for the month previous of the month I am looking for to put that clients data into the record set.

I was thinking of expanding the values for the BETWEEN statement then doing a comparison in vbscript but that would start to get bulky again, and I’m almost sure this could be done all in SQL.

Thanks in advance for any help with this issue.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
closetosane is offline Offline
12 posts
since Jan 2009
Jul 19th, 2009
0

Re: Need to get data for months no payment was made

Problem Solved:
MySQL Syntax (Toggle Plain Text)
  1. strSQL = " SELECT C.C_LName, C.C_FName, Max(P.P_Month) AS dtLastPayment "
  2. strSQL = strSQL & " FROM (Client AS C INNER JOIN Payment_Board AS P "
  3. strSQL = strSQL & " ON C.C_SIN = P.P_SIN) "
  4. strSQL = strSQL & " INNER JOIN Pt AS Pt ON P.P_ID = Pt.Pt_ID "
  5. strSQL = strSQL & " WHERE C.C_Store_No =" & Store_Numb
  6. strSQL = strSQL & " AND P.P_Amount_Paid = Pt.Pt_Amount "
  7. strSQL = strSQL & " AND C.C_Delete <>" & 1
  8. strSQL = strSQL & " AND Pt.Pt_Type =" & "'" & SetMonthlyPayment & "'"
  9. strSQL = strSQL & " GROUP BY C.C_LName, C.C_FName "

It's not prefect, but it runs in a fraction of the time and is easy to sort on the webpage. The tablename Pt has been shortened to fit on a single line for this post.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
closetosane is offline Offline
12 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: import-export mysql coding
Next Thread in MySQL Forum Timeline: SQL to return unique results based on 2 fields





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC