seeking help what select statement should i write.

  • i have rental data store at this table structure

    PropertyTenantIDTrx TypeEFFDATEENDDATE       AMOUNT 
    ATenant ABR20/08/201819/08/201926,695.19 
    ATenant ABR20/08/201919/08/202031,406.1 
    ATenant APF 20/08/201519/08/20201,256.24 
    ATenant ASC 20/08/201519/08/20204,396.85 

    How can i achieve the result in the follow matter, many thanks.

    PropertyTenantIDEFFDATEENDDATEBRPFSC
    ATenant A20/08/201831/08/2018              15,379.77                   445.76            1,560.17
    ATenant A01/09/201830/09/2018              26,695.19                1,256.24            4,396.85
    ATenant A01/10/201831/10/2018              26,695.19                1,256.24            4,396.85
    ATenant A01/11/201830/11/2018              26,695.19                1,256.24            4,396.85
    ATenant A01/12/201831/12/2018              26,695.19                1,256.24            4,396.85
    ATenant A01/01/201931/01/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/02/201928/02/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/03/201931/03/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/04/201930/04/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/05/201931/05/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/06/201930/06/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/07/201931/07/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/08/201931/08/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/09/201930/09/2019              27,505.67                1,256.24            4,396.85
    ATenant A01/10/201931/10/2019              31,406.10                1,256.24            4,396.85

    ATenant A01/10/201931/10/2019              31,406.10                1,256.24            4,396.85

    until the end of the last month of the tenancy like this

    ATenant A01/08/202019/08/2020              26,695.19                   769.95            2,694.84
  • Welcome to SSC. What have you tried so far to resolve this yourself? Could you post what you have done so far and where are you having difficulty? The users here are happy to help, but we're not here to do your work for you.

    You haven't explained your logic, however, but I think that you could achieve what you're after with Calendar Table: http://www.sqlservercentral.com/articles/calendar/145206/. Have a read of the article, and if your unsure how to use it, reply with the logic you need, (and what you've tried), and I'll be happy to help further.

    If you want to really go the "whole hog", check the link in my signature on how to provide DDL for your tables and insert statements for your sample data.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • This was done by friend of mine. Some data was return as duplicate especially for those period start in the mid month. hence, looking help how can enhance it based on result above.

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[asp_tableaurent]
    AS

    TRUNCATE TABLE tableaurent;

    DECLARE @MonthStep    INT;

    DECLARE @t_UID        nvarchar(255);
    DECLARE    @t_BLDGID    nvarchar(200);
    DECLARE    @t_LEASID    nvarchar(200);
    DECLARE    @t_INCCAT    nvarchar(200);
    DECLARE    @t_EFFDATE    datetime;
    DECLARE    @t_ENDDATE    datetime;
    DECLARE @t_AMOUNT    money;
    DECLARE    @t_DTDIFF    int;

    DECLARE trans_cur CURSOR FOR
    SELECT z.BLDGID, z.LEASID, z.INCCAT, z.EFFDATE, z.ENDDATE, z.AMOUNT, DATEDIFF(month, EFFDATE, ENDDATE) as DTDiff
    FROM CMRECC z
    WHERE z.INCCAT IN ('FR','SC','PF');

    OPEN trans_cur;
    FETCH NEXT FROM trans_cur INTO @t_BLDGID, @t_LEASID, @t_INCCAT, @t_EFFDATE, @t_ENDDATE, @t_AMOUNT, @t_DTDIFF;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @MonthStep = 0
        SET @t_UID = NEWID();
        WHILE @MonthStep < @t_DTDIFF
        BEGIN
        
            INSERT INTO tableaurent VALUES (@t_UID, @t_BLDGID, @t_LEASID, @t_INCCAT, @t_EFFDATE, @t_ENDDATE, @t_AMOUNT, @t_DTDIFF, DATEPART(month, DATEADD(month,@MonthStep,@t_EFFDATE)), DATEPART(year, DATEADD(month,@MonthStep,@t_EFFDATE)), NULL );

            SET @MonthStep = @MonthStep + 1

            IF @MonthStep = @t_DTDIFF
            BEGIN
                IF DATEPART(Month, @t_ENDDATE) = DATEPART(month, DATEADD(month,@MonthStep,@t_EFFDATE))
                BEGIN
                    INSERT INTO tableaurent VALUES (@t_UID, @t_BLDGID, @t_LEASID, @t_INCCAT, @t_EFFDATE, @t_ENDDATE, @t_AMOUNT, @t_DTDIFF, DATEPART(month, DATEADD(month,@MonthStep,@t_EFFDATE)), DATEPART(year, DATEADD(month,@MonthStep,@t_EFFDATE)), NULL );
                END
            END
        END
        FETCH NEXT FROM trans_cur INTO @t_BLDGID, @t_LEASID, @t_INCCAT, @t_EFFDATE, @t_ENDDATE, @t_AMOUNT, @t_DTDIFF;
    END

    CLOSE trans_cur;
    DEALLOCATE trans_cur;

    SELECT * FROM tableaurent;

  • crazysf - Tuesday, August 7, 2018 2:32 AM

    i have rental data store at this table structure

    PropertyTenantIDTrx TypeEFFDATEENDDATE       AMOUNT 
    ATenant ABR20/08/201819/08/201926,695.19 
    ATenant ABR20/08/201919/08/202031,406.1 
    ATenant APF 20/08/201519/08/20201,256.24 
    ATenant ASC 20/08/201519/08/20204,396.85 

    How can i achieve the result in the follow matter, many thanks.

    PropertyTenantIDEFFDATEENDDATEBRPFSC
    ATenant A20/08/201831/08/2018              15,379.77                   445.76            1,560.17
    ATenant A01/09/201830/09/2018              26,695.19                1,256.24            4,396.85
    ATenant A01/10/201831/10/2018              26,695.19                1,256.24            4,396.85
    ATenant A01/11/201830/11/2018              26,695.19                1,256.24            4,396.85
    ATenant A01/12/201831/12/2018              26,695.19                1,256.24            4,396.85
    ATenant A01/01/201931/01/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/02/201928/02/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/03/201931/03/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/04/201930/04/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/05/201931/05/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/06/201930/06/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/07/201931/07/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/08/201931/08/2019              26,695.19                1,256.24            4,396.85
    ATenant A01/09/201930/09/2019              27,505.67                1,256.24            4,396.85
    ATenant A01/10/201931/10/2019              31,406.10                1,256.24            4,396.85

    ATenant A01/10/201931/10/2019              31,406.10                1,256.24            4,396.85

    until the end of the last month of the tenancy like this

    ATenant A01/08/202019/08/2020              26,695.19                   769.95            2,694.84

    This CAN be done using set-based methods, as you do NOT need a cursor.   However, we'd need to know how, exactly, to pro-rate partial months, as it's clearly NOT a case of just using the number of days in the month that are actually used for the particular partial month, and it even appears that the final partial month is not pro-rated.   Also, I tried to see if the PF and SC columns were a straight percentage, and that's clearly not the case either, so we'd need those calculations as well.   Post back with the details.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

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

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