Help modifying SUM

  • Table Structure

    DCR

    ------

    CustomerNumber

    CC

    PDC

    EnteredDate

    FeeGoal

    FeeSchedule

    PostedAmount

    I need a query which will do:

    SUM(PostedAmount + SUM(All PDC where EnteredDate in current month) + SUM(All CC where entered date in current month) + SUM(All PDC where EnteredDate not in current month) + SUM(All CC where entered date in current month)) * FeeSchedule

    for each company

    call that In-House for the above Sum

    Then I need to do a grand total on all In-House as one sum.  I did something similar like this for another calculation but it didn't include the additions of PDC and CC.  That query looked like this:

    Select Total = SUM(gt)

    FROM

    (Select Distinct CustomerName, CustomerNumber, FeeGoal AS FG, FeeSchedule,

                    (cast(FeeGoal as numeric(30,10)) / FeeSchedule) * 100 AS gt

    from DCR WHERE branch='00002'

    group by CustomerName,

                 CustomerNumber,

                 FeeGoal,

                 FeeSchedule

    ) as dTable

    so really what I want to change is (cast(FeeGoal as numeric(30,10)) / FeeSchedule) * 100  to add the PDC and CC sums and then * FeeSchedule instead of / like I have here

  • Can you post your table DDL, some sample data and expected result ?

  • What's the point in calculating

    SUM(All PDC where EnteredDate in current month) + SUM(All PDC where EnteredDate not in current month)

    Isn't that simply SUM(All PDC)?

    You write "for each company", but I don't see company in your table. Perhaps you mean CustomerNumber?

  • You did not include enough information for an actual solution to be presented. However, the following example shows you how to conditionally SUM a column's value, using a CASE statement:

    SELECT [CustomerNumber]

         , SUM(PostedAmount

            + SUM(CASE WHEN MONTH([EnteredDate])=MONTH(GETDATE()) THEN [PDC] ELSE 0 END)

            + SUM(CASE WHEN MONTH([EnteredDate])=MONTH(GETDATE()) THEN [CC] ELSE 0 END)

            AS Total

    I hope this helps.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply