"Payment Plan Manager" Query Help
Posted: Sun Mar 13, 2011 2:47 pm
I made a new module with an interactive "Payment Plan Manager" for my plugin. Currently, it loads similar data to the built in static payment plan report, except it is interactive using the same copy of the "Treatment Finder" windows so it loads the pt Account when clicking on a row.
I am trying to add more data, but pretty much a newbie at writing MySQL queries. Could I get some help here?
i am trying to load 1) the amt (principal + interest) and 2)date of the Latest due payplancharge only...so the last row <= to today's date.
I figured out the amt query in SQLyog, but when I put the code in the code block and debug I am having difficulty in that it tells me can't find a field for the subquery. (does a temp table not have the ability to use other fields from the query?)
The date one I am still stumped on, it keeps returning a minimum date of 01/01/0001....so I think my codes was wrong...but don't really know how to get what I want.
Here is what I have that works
} string command=@"SELECT FName,LName,MiddleI,Preferred, PlanNum,patient.PatNum,
(SELECT SUM(Principal+Interest) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@") ""_accumDue"",
(SELECT SUM(Principal+Interest) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+DbHelper.DateAddDay(datesql,POut.Long(PrefC.GetLong(PrefName.PayPlansBillInAdvanceDays)))+@") ""_dueTen"",
(SELECT SUM(SplitAmt) FROM paysplit WHERE paysplit.PayPlanNum=payplan.PayPlanNum) ""_paid"",
(SELECT SUM(Principal) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum) ""_principal""
FROM payplan
LEFT JOIN patient ON patient.PatNum=payplan.Guarantor "
//WHERE SUBSTRING(Birthdate,6,5) >= '"+dateFrom.ToString("MM-dd")+"' "
//+"AND SUBSTRING(Birthdate,6,5) <= '"+dateTo.ToString("MM-dd")+"' "
+"GROUP BY FName,LName,MiddleI, Preferred, payplan.PayPlanNum ORDER BY LName,FName";
When I add the italicizes line it says payplan.PayPlanNum not found, but it works in SQLyog...why the difference?(does a temp table not have the ability to use other fields from the query?)
} string command=@"SELECT FName,LName,MiddleI,Preferred, PlanNum,patient.PatNum,
(SELECT SUM(Principal+Interest) FROM (SELECT Principal, Interest FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@" ORDER BY ChargeDate DESC LIMIT 1) tmp1) ""_latestDueAmt"",
(SELECT SUM(Principal+Interest) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@") ""_accumDue"",
(SELECT SUM(Principal+Interest) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+DbHelper.DateAddDay(datesql,POut.Long(PrefC.GetLong(PrefName.PayPlansBillInAdvanceDays)))+@") ""_dueTen"",
(SELECT SUM(SplitAmt) FROM paysplit WHERE paysplit.PayPlanNum=payplan.PayPlanNum) ""_paid"",
(SELECT SUM(Principal) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum) ""_principal""
FROM payplan
LEFT JOIN patient ON patient.PatNum=payplan.Guarantor "
//WHERE SUBSTRING(Birthdate,6,5) >= '"+dateFrom.ToString("MM-dd")+"' "
//+"AND SUBSTRING(Birthdate,6,5) <= '"+dateTo.ToString("MM-dd")+"' "
+"GROUP BY FName,LName,MiddleI, Preferred, payplan.PayPlanNum ORDER BY LName,FName";
Also, this is the other subquery I am trying to use and getting a min date result of 01/01/0001 when I run the code
datesql="CURDATE()"
(SELECT ChargeDate FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@" ORDER BY ChargeDate DESC LIMIT 1) ""_latestDueDate"",
reading online I think the above is completely wrong and why I am getting a bad date. This might be more what I want (but would have the same result as above I think)
datesql="CURDATE()"
(SELECT MAX(ChargeDate) FROM (SELECT ChargeDate FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@" ORDER BY ChargeDate DESC) tmp2) ""_latestDueDate"",
Any help would be appreciated...thanks in advance.
Do I use Join instead of temp tables?
I am trying to add more data, but pretty much a newbie at writing MySQL queries. Could I get some help here?
i am trying to load 1) the amt (principal + interest) and 2)date of the Latest due payplancharge only...so the last row <= to today's date.
I figured out the amt query in SQLyog, but when I put the code in the code block and debug I am having difficulty in that it tells me can't find a field for the subquery. (does a temp table not have the ability to use other fields from the query?)
The date one I am still stumped on, it keeps returning a minimum date of 01/01/0001....so I think my codes was wrong...but don't really know how to get what I want.
Here is what I have that works
} string command=@"SELECT FName,LName,MiddleI,Preferred, PlanNum,patient.PatNum,
(SELECT SUM(Principal+Interest) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@") ""_accumDue"",
(SELECT SUM(Principal+Interest) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+DbHelper.DateAddDay(datesql,POut.Long(PrefC.GetLong(PrefName.PayPlansBillInAdvanceDays)))+@") ""_dueTen"",
(SELECT SUM(SplitAmt) FROM paysplit WHERE paysplit.PayPlanNum=payplan.PayPlanNum) ""_paid"",
(SELECT SUM(Principal) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum) ""_principal""
FROM payplan
LEFT JOIN patient ON patient.PatNum=payplan.Guarantor "
//WHERE SUBSTRING(Birthdate,6,5) >= '"+dateFrom.ToString("MM-dd")+"' "
//+"AND SUBSTRING(Birthdate,6,5) <= '"+dateTo.ToString("MM-dd")+"' "
+"GROUP BY FName,LName,MiddleI, Preferred, payplan.PayPlanNum ORDER BY LName,FName";
When I add the italicizes line it says payplan.PayPlanNum not found, but it works in SQLyog...why the difference?(does a temp table not have the ability to use other fields from the query?)
} string command=@"SELECT FName,LName,MiddleI,Preferred, PlanNum,patient.PatNum,
(SELECT SUM(Principal+Interest) FROM (SELECT Principal, Interest FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@" ORDER BY ChargeDate DESC LIMIT 1) tmp1) ""_latestDueAmt"",
(SELECT SUM(Principal+Interest) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@") ""_accumDue"",
(SELECT SUM(Principal+Interest) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+DbHelper.DateAddDay(datesql,POut.Long(PrefC.GetLong(PrefName.PayPlansBillInAdvanceDays)))+@") ""_dueTen"",
(SELECT SUM(SplitAmt) FROM paysplit WHERE paysplit.PayPlanNum=payplan.PayPlanNum) ""_paid"",
(SELECT SUM(Principal) FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum) ""_principal""
FROM payplan
LEFT JOIN patient ON patient.PatNum=payplan.Guarantor "
//WHERE SUBSTRING(Birthdate,6,5) >= '"+dateFrom.ToString("MM-dd")+"' "
//+"AND SUBSTRING(Birthdate,6,5) <= '"+dateTo.ToString("MM-dd")+"' "
+"GROUP BY FName,LName,MiddleI, Preferred, payplan.PayPlanNum ORDER BY LName,FName";
Also, this is the other subquery I am trying to use and getting a min date result of 01/01/0001 when I run the code
datesql="CURDATE()"
(SELECT ChargeDate FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@" ORDER BY ChargeDate DESC LIMIT 1) ""_latestDueDate"",
reading online I think the above is completely wrong and why I am getting a bad date. This might be more what I want (but would have the same result as above I think)
datesql="CURDATE()"
(SELECT MAX(ChargeDate) FROM (SELECT ChargeDate FROM payplancharge WHERE payplancharge.PayPlanNum=payplan.PayPlanNum
AND ChargeDate <= "+datesql+@" ORDER BY ChargeDate DESC) tmp2) ""_latestDueDate"",
Any help would be appreciated...thanks in advance.
Do I use Join instead of temp tables?