Home > Back-end >  How to add revenue calculation to existing query for MTD,YTD and QTD
How to add revenue calculation to existing query for MTD,YTD and QTD

Time:07-31

I have a query that works perfectly but I need somehow need to add a calculation of the following for DT,YTD,QTD. The calculation has to be NetRentals = result.MoveIns - result.MoveOuts result.Transfers result.MoveOutUndo result.NonRevenueMI;. But I need to have it for DT,YTD,QTD.

Mov-in/Move-outs

DECLARE @EndDate As Date
DECLARE @Props as int
     
SELECT
    'Move-Ins' strType,
    IsNull(SUM(CASE WHEN dtReport = @EndDate THEN intMoveIn ELSE 0 END),0) AS intDT,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfMonth AND @EndDate THEN intMoveIn ELSE 0 END),0) AS intMTD,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfQuarter AND @EndDate THEN intMoveIn ELSE 0 END),0) AS intQTD,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfYear AND @EndDate THEN intMoveIn ELSE 0 END),0) AS intYTD
FROM tblUBMReport WITH (NOLOCK)
INNER JOIN tblDate WITH (NOLOCK) ON tblDate.dtDate = @EndDate
WHERE intProp IN (@Props) AND dtReport BETWEEN @StartDate AND @EndDate
    
UNION

SELECT
    'Move-Outs' AS strType,
    IsNull(SUM(CASE WHEN dtReport = @EndDate THEN intMoveOut ELSE 0 END),0) AS intDT,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfMonth AND @EndDate THEN intMoveOut ELSE 0 END),0) AS intMTD,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfQuarter AND @EndDate THEN intMoveOut ELSE 0 END),0) AS intQTD,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfYear AND @EndDate THEN intMoveOut ELSE 0 END),0) AS intYTD
FROM tblUBMReport WITH (NOLOCK)
INNER JOIN tblDate WITH (NOLOCK) ON tblDate.dtDate = @EndDate
WHERE intProp IN (@Props) AND dtReport BETWEEN @StartDate AND @EndDate
    
UNION

SELECT
    'Transfers' AS strType,
    IsNull(SUM(CASE WHEN dtReport = @EndDate THEN intTransfer ELSE 0 END),0) AS intDT,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfMonth AND @EndDate THEN intTransfer ELSE 0 END),0) AS intMTD,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfQuarter AND @EndDate THEN intTransfer ELSE 0 END),0) AS intQTD,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfYear AND @EndDate THEN intTransfer ELSE 0 END),0) AS intYTD
FROM tblUBMReport WITH (NOLOCK)
INNER JOIN tblDate WITH (NOLOCK) ON tblDate.dtDate = @EndDate
WHERE intProp IN (@Props) AND dtReport BETWEEN @StartDate AND @EndDate
    
UNION

SELECT
    'Non-Revenue Move_Ins' AS strType,
    ISNULL(SUM(CASE WHEN dtReport = @EndDate THEN intNonRevenueMI ELSE 0 END),0) AS intDT,
    ISNULL(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfMonth AND @EndDate THEN intNonRevenueMI ELSE 0 END),0) AS intMTD,
    ISNUll(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfQuarter AND @EndDate THEN intNonRevenueMI ELSE 0 END),0) AS intQTD,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfYear AND @EndDate THEN intNonRevenueMI ELSE 0 END),0) AS intYTD
FROM tblUBMReport WITH (NOLOCK)
INNER JOIN tblDate WITH (NOLOCK) ON tblDate.dtDate = @EndDate
WHERE intProp IN (@Props) AND dtReport BETWEEN @StartDate AND @EndDate
    
UNION

SELECT
    'Move_out Undo' AS strType,
    IsNull(SUM(CASE WHEN dtReport = @EndDate THEN intNonRevenueMI ELSE 0 END),0) AS intDT,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfMonth AND @EndDate THEN intMoveOutUndo ELSE 0 END),0) AS intMTD,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfQuarter AND @EndDate THEN intMoveOutUndo ELSE 0 END),0) AS intQTD,
    IsNull(SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfYear AND @EndDate THEN intMoveOutUndo ELSE 0 END),0) AS intYTD
FROM tblUBMReport WITH (NOLOCK)
INNER JOIN tblDate WITH (NOLOCK) ON tblDate.dtDate = @EndDate
WHERE intProp IN (@Props) AND dtReport BETWEEN @StartDate AND @EndDate

CodePudding user response:

If you're using SSRS, you would do this in the table and not the query.

Normally you would use =SUM(Fields!intDT.Value) but a different calcualtion is needed.

NetRentals = result.MoveIns - result.MoveOuts result.Transfers result.MoveOutUndo result.NonRevenueM

Since the fields you sum are the same but add or subtract based on strType:

=SUM(Fields!intDT.Value * IIF(Fields!strType.Value = "Move-Outs", -1, 1))

Of course the intDT would need to be changed for the other date ranges.

CodePudding user response:

My first thought would have been to have fed the initial query through a GROUP BY ... WITH ROLLUP, but this would not work with the Move-Outs values unless you are willing to negate the sign in the original calculation.

The alternative is to add a sign column (1 or -1) to your original query and then feed that via a CTE (Common Table Expression) into another query that performs a UNION of the initial results with the calculated sums.

The result would be something like:

--- CTE with sign feeding another UNION ALL to combine details with total
WITH CTE AS (
    -- Insert the original query here. The data below is a placeholder.
    -- The Sign column has been added to feed the Net calculation.
    -- Ord has also been added to explicity define result ordering.
    SELECT *
    FROM (
        VALUES
            (1,  1, 'Move-Ins',             566, 13461, 13461, 93285),
            (2, -1, 'Move-Outs',            619, 11797, 11797, 78394),
            (3,  1, 'Transfers',             18,   522,   522,  3805),
            (4,  1, 'Non-Revenue Mone-Ins',   1,    40,    40,   315),
            (5,  1, 'Move-Out Undo',          9,   209,   209,  1404)
    ) DATA(Ord, Sign, Type, DT, MTD, QTD, YTD)
)
SELECT Ord, Type, DT, MTD, QTD, YTD
FROM CTE
UNION ALL
SELECT Ord = 6, 'Net Rentals:',
    SUM(DT * Sign), SUM(MTD * Sign), SUM(QTD * Sign), SUM(YTD * Sign)
FROM CTE
ORDER BY Ord

See the following db<>fiddle for a working example. I also included versions that use negated Move-Outs calculations and GROUP BY ... WITH ROLLUP.

  • Related