Skip to main content
Inspiring
October 29, 2008
Question

Subract sums of two queries from each other to get subtotal

Once again, I'm in over my head - help help!
I have been struggling with this off and on over the last couple weeks and I'm not sure to look for a solution.
Here's what I'm trying to do:

Schools send checks covering workshop fees, multiple registrations per workshop and per school. Subtotal the amounts of the checks per school. Add the amounts of the paid registration fees per school, and subtract the subtotal from the subtotal of checks. Display the result.

I feel like I'm going in the right direction, but I'm obviously missing something essential because I get this error:

The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code.

Null Pointers are another name for undefined values.

The error occurred in C:\Inetpub\wwwroot\crtis\admin\schoolFin\allSchoolFin_credit.cfm: line 65

63 : </cfquery>
64 :
65 : <cfquery name="getSubTotal" dbtype="query">
66 : SELECT getSTChecks.s_SchoolName AS s_SchoolName, getSTWSCost.s_SchoolName, SUM(getSTChecks.STChecks - getSTWSCost.STWSCost) AS SubTotal
67 : FROM getSTChecks, getSTWSCost

I would be grateful for either a nudge toward some reading (CFWACK?) or if someone wants to take a look at my code and make a suggestion, that would be most welcome.
    Ce sujet a été fermé aux réponses.

    11 commentaires

    Inspiring
    October 31, 2008
    You are very welcome! Happy Halloween to you too.

    Cheers
    Inspiring
    October 31, 2008
    >Okay. As long as you are aware of the issue. I just wanted to be sure it would not take you by surprise two months down the road ;-)

    I do appreciate that :) I would rather think about it now than be slapping my forehead later!

    Once again, you have been *exceedingly* helpful and delightful. Thank you so much, and Happy Hallowe'en!
    Inspiring
    October 31, 2008
    >So ... change the workshop cost .. and you instantly change those totals.

    The cost wouldn't change for the workshops that were already attended and marked paid. If we told them that the workshop was $12, it would stay $12. (This is a Catholic organization - very, *very* little changes around here ;D)

    I do understand what you're saying about totals changing; mistakes are sometimes made - I recently set up something for invoicing, and when we printed and sent them out, one of the workshop sessions was incorrectly priced, so some of the invoices were wrong - but it was a simple thing to fix, since we're in constant communication with the schools (there are a little over 60 of them, but usually no more than 5 or so are represented at a workshop). In this case, we are offering them a check for the overage, or credit toward other workshops (which is another one of the reasons I'm trying to work out something that shows the funds left over, just in case). It happens very seldom, only once in the 3 years I've been here, and it only affected about three schools.

    If something like this happened after we already applied payments sent, it would just mean (if the workshop changed from higher to lower) that the schools would have extra money to apply toward future workshops, or (if the workshop changed from lower to higher) if it was our mistake, we would probably eat it. They all will be sending teachers in the future, so it isn't a question of whether or not they'll want to take a workshop again!

    >It sounds like it would probably work out in most cases, except if the check amount was wrong ;-)

    If the check amount is wrong, there are a few different options. We can send the check back and request the correct amount, or we can keep the check, only record the correct amount (we are not accounting or finance so if I only record the amount of the check we're going to use it's fine) and send them a check for the overage. In this case I usually make a note in the 'Memo' field with the actual amount of the check.
    Once again, I realize this wouldn't work for a "real world" application - but like I said, if I ever have the opportunity (and skill!) to develop something like that, I would take all of those things into account. I guess it would be good practice now to do it, but frankly, I don't have the luxury of time to worry about it. The workshops keep happening, the checks are piling up! and I'm also in charge of tracking and correcting the registrations, adding new teachers to the db, posting the workshops to the website, etc., etc. - and in the mean time, building something that will allow me to do this all from an interface instead of directly with the db.
    Inspiring
    October 31, 2008
    > I do understand what you're saying about totals changing;
    > ..
    > Once again, I realize this wouldn't work for a "real world" application - but like I said, if I ever
    > have the opportunity (and skill!) to develop something like that, I would take all of those things
    > into account. I guess it would be good practice now to do it, but frankly, I don't have the luxury
    > of time to worry about it.


    Okay. As long as you are aware of the issue. I just wanted to be sure it would not take you by surprise two months down the road ;-)
    Inspiring
    October 30, 2008
    >It would be easy to over-allocate check payments ...
    >Do you actually have a way of distinguishing between checks that were already allocated...

    I have a large pile of paper (registrations and copies of checks) with notes on them! :D The goal of this project is to be able determine the allocation with SQL. Unfortunately I am in the position of both having to do the work that the application I am building will eventually do, *and* building the application. The silver lining is that I think I'll build something very useful to the person who steps in after me (who will possibly not be a web designer, but more of an admin) because I will know what they will need to get the work done efficiently.

    It seems like, in the long run, I wouldn't need the 'Paid' field, because I could use the 'WorkshopRegPaymentID' field to indicate payment. Until then, though, I need to keep it because in another part of this project, I'm displaying rosters that show 'paid' and 'completed' that involve that field.

    I'm using MS SQL for the active database. I do some work with Access locally.
    Inspiring
    October 30, 2008
    Sorry for the delay - was in a meeting this a.m.!

    I wanted to keep the payments (check info) separate from the registration info, because sometimes the schools send checks to cover multiple workshop events. So that's why the check info would be linked through the registrant table (many-to-one relationship).

    The final goal is to create a page where I will display all the unpaid registrations for a school, and also all the available funds that I could use to pay those registrations; I would select the registrations I want to pay for, then select the check I want to use to pay for them.

    As I'm writing this, it occurred to me that I really do need, at that point, to show the funds available from each check, not the total amount, since I want to attribute specific payment records to various registration records. But the page you're helping me with is necessary to show that there is a certain amount of funds available from whatever combined check amounts not already used. At some point this will become available for viewing by the schools so that they can see if they have available credit due to overpayment or if someone doesn't attend who was paid for.
    Inspiring
    October 30, 2008
    > I wanted to keep the payments (check info) separate from the registration info, because sometimes
    > the schools send checks to cover multiple workshop events.

    Yes, that makes sense. I think what feels off to me is the "WorkshopPaid" flag. It would be easy to over-allocate check payments by marking too many records as "paid". In other words, mark 10 registrants as paid (at $100 each) when the total check amount was only $500.00.

    > But the page you're helping me with is necessary to show that there is a certain amount of funds
    > available from whatever combined check amounts not already used

    Do you actually have a way of distinguishing between checks that were already allocated and ones that have a remaining balance .. or are you trying to determine that on-the-fly using sql?

    What database are you using?

    Inspiring
    October 30, 2008
    > It seems like, in the long run, I wouldn't need the 'Paid' field

    Well, the bigger issue is that there is no detailed accounting of which amounts were applied when. So small changes (like adjusting the WorkShop cost amount) could have an unintended cascade effect. But another question is what do you do with wrong or uneven check amounts? Say a school sends a check for $532.62. But the workshops for their employees only cost $500.00. There is $32.62 left over. Since you are not storing amounts, how do you apply the remaining amount ... or can you even apply it?

    > I'm using MS SQL for the active database

    To just calculate the overall totals per school, I would probably create two views. Something like the attached. Once you have them, you can join them together to get the totals.

    BTW, you do not have to use views. But if you are going to reuse the query logic, it makes more sense to write it once as a view. Then you can reuse it many times.







    Inspiring
    October 30, 2008
    > FROM WorkshopRegistration as wr
    > LEFT JOIN WorkshopRegPayment AS wrp ON
    > wr.WorkshopRegPaymentID = wrp.WorkshopRegPaymentID

    Hmm... on second thought ... I am missing a connection. If payments are only linked through the registrants table, then what is the difference between these two values:

    > Subtotal the amounts of the checks per school. Add the amounts of the > paid registration fees per school





    Inspiring
    October 29, 2008
    Oh dear - I just checked something I should have checked before -

    I tried outputting just the check amount subtotals, and they're coming up goose eggs - so I need to play with this a little more. I will re-post the code when I figure out why.
    Inspiring
    October 29, 2008
    I had to separate the check amounts from the session cost query - it seems like the WHERE statement was resulting in the goose eggs.

    Updated code:
    Inspiring
    October 30, 2008

    > Schools send checks covering workshop fees, multiple registrations per workshop
    > and per school. Subtotal the amounts of the checks per school. Add the amounts of
    > the paid registration fees per school, and subtract the subtotal from the subtotal
    > of checks. Display the result.

    Can you describe the workshop tables involved, and post a small sample of the pertinent values to show how they related?

    WorkshopEvent
    WorkshopEventSessions
    WorkshopRegistration
    WorkshopRegPayment

    Inspiring
    October 29, 2008
    That would be lovely. I'm all for simpler ways of doing things!
    Inspiring
    October 29, 2008
    Glad it is working now.

    If you do not need those extra columns, I am relatively certain there is a simpler query that could give you the totals. If I get a chance to review the sql in greater depth later, I may post some suggestions.

    Cheers



    Inspiring
    October 29, 2008
    > Still getting that pesky '"wr.WorkshopSessionID" could not
    > be bound' error.)

    > FROM WorkshopRegistration as wr, WorkshopRegPayment as wrp

    You are missing the LEFT JOIN .. INNER JOIN ... syntax between those two tables. So the database is probably getting confused. Try reverting to what you had in your original query:

    FROM WorkshopRegistration as wr
    LEFT JOIN WorkshopRegPayment AS wrp
    ON wr.WorkshopRegPaymentID = wrp.WorkshopRegPaymentID
    ....


    But you still need to add COALESCE(..) around the columns used in the QoQ SUM (ie wrp_CheckAmount, wes_WorkshopSessionCost) to try and avoid the nullpointerexception.