Loop over all in table Current Month and Prior Month for Change Report

  • Hope I explain this correctly.  I have the following query that is looking at a specific record but I have around 65K records to loop over

    select * from Contract_priormonth C
    where c.C_SSKey = '614-P085453-P'
    and DS_SysEnd = (select max(DS_SysEnd) from Contract_priorMonth where C_SSKey = '614-P085453-p' group by C_SSKey)

    What it does is find the Max Date of a contract entry into the temporal database table for contracts.  I have been charged with creating a Change report with the following Parameters.  They want the Current Month (which I have those easily enough) and the latest changes prior to EOM last month.  So for example  Want the current month (March) and the record closest to the end of the month in Feb So here is a query that has multiple values in it.  
    Here is some data to loop over or look at.  I have Current Month and Prior Month as Temp tables with some dummy data. Current month is notated by DS_SysEnd being 9999-12-21 23:59:59.0000000
    ** What I need to is join the Current Month and the Prior Month together and get the changes from the Current Month to the PriorMonth and list those side by side.  Also loop through all of the contracts and return results if there are any.

    If(OBJECT_ID('tempdb..#PriorMonth') Is Not Null)
    Begin
      Drop Table #PriorMonth
    End
    IF(Object_id('tempdb..#CurrentMonth') Is Not Null)
    Begin
      Drop Table #CurrentMonth
    End

    Create Table #PriorMonth (
    C_SSKey nvarchar(15),
    DS_SysStart datetime2,
    DS_SysEnd datetime2,
    ContractNumber nvarchar(7),
    Zone nvarchar(3)
    )

    insert into #PriorMonth values ('614-P085453-P', '2017-11-30 01:00:26.5230031', '2018-02-28 19:00:46.6000000', 'P085453', 'PAT')
    insert into #PriorMonth values ('614-P085453-P', '2017-09-19 23:00:05.0000000', '2017-09-29 22:32:58.8450140', 'P085453', 'PAT')
    insert into #PriorMonth values ('614-P085453-P', '2017-09-29 22:32:58.8450140', '2017-09-29 22:32:58.8450140', 'P085453', 'LAT')
    insert into #PriorMonth values ('614-P085199-P', '2017-07-28 16:15:15.0000000', '2017-07-28 16:15:15.0000000', 'P085199', 'HTA')
    insert into #PriorMonth values ('614-P085199-P', '2017-09-19 23:00:05.0000000', '2018-02-12 19:00:48.6300000', 'P085199', 'HTA')

    Create Table #CurrentMonth (
    C_SSKey nvarchar(15),
    DS_SysStart datetime2,
    DS_SysEnd datetime2,
    ContractNumber nvarchar(7),
    Zone nvarchar(3)
    )

    insert into #CurrentMonth values('614-P085199-P', '2018-02-12 19:00:48.6300000', '9999-12-31 23:59:59.0000000', 'P085199', 'JRK')
    insert into #CurrentMonth values('614-P085453-P', '2018-02-28 19:00:46.6000000', '9999-12-31 23:59:59.0000000', 'P085453', 'PAT')

    Select * from #CurrentMonth
    Select * From #PriorMonth

    Any help would be greatly appreciated
    Thanks

  • ;with cte as
    (
     Select C_SSKey, MAX(DS_SysEnd) MaxDate
     from #PriorMonth
     group by C_SSKey
    )
    select c.* , cte.MaxDate
    from #CurrentMonth C
    inner join cte on cte.C_SSKey = c.C_SSKey

    Does this work? If not, please specify how you want the output to look given your test data.

    __________________________________________________________________________________________________________
    How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Stephen crocker - Monday, March 5, 2018 8:48 AM

    Hope I explain this correctly.  I have the following query that is looking at a specific record but I have around 65K records to loop over

    select * from Contract_priormonth C
    where c.C_SSKey = '614-P085453-P'
    and DS_SysEnd = (select max(DS_SysEnd) from Contract_priorMonth where C_SSKey = '614-P085453-p' group by C_SSKey)

    What it does is find the Max Date of a contract entry into the temporal database table for contracts.  I have been charged with creating a Change report with the following Parameters.  They want the Current Month (which I have those easily enough) and the latest changes prior to EOM last month.  So for example  Want the current month (March) and the record closest to the end of the month in Feb So here is a query that has multiple values in it.  
    Here is some data to loop over or look at.  I have Current Month and Prior Month as Temp tables with some dummy data. Current month is notated by DS_SysEnd being 9999-12-21 23:59:59.0000000
    ** What I need to is join the Current Month and the Prior Month together and get the changes from the Current Month to the PriorMonth and list those side by side.  Also loop through all of the contracts and return results if there are any.

    If(OBJECT_ID('tempdb..#PriorMonth') Is Not Null)
    Begin
      Drop Table #PriorMonth
    End
    IF(Object_id('tempdb..#CurrentMonth') Is Not Null)
    Begin
      Drop Table #CurrentMonth
    End

    Create Table #PriorMonth (
    C_SSKey nvarchar(15),
    DS_SysStart datetime2,
    DS_SysEnd datetime2,
    ContractNumber nvarchar(7),
    Zone nvarchar(3)
    )

    insert into #PriorMonth values ('614-P085453-P', '2017-11-30 01:00:26.5230031', '2018-02-28 19:00:46.6000000', 'P085453', 'PAT')
    insert into #PriorMonth values ('614-P085453-P', '2017-09-19 23:00:05.0000000', '2017-09-29 22:32:58.8450140', 'P085453', 'PAT')
    insert into #PriorMonth values ('614-P085453-P', '2017-09-29 22:32:58.8450140', '2017-09-29 22:32:58.8450140', 'P085453', 'LAT')
    insert into #PriorMonth values ('614-P085199-P', '2017-07-28 16:15:15.0000000', '2017-07-28 16:15:15.0000000', 'P085199', 'HTA')
    insert into #PriorMonth values ('614-P085199-P', '2017-09-19 23:00:05.0000000', '2018-02-12 19:00:48.6300000', 'P085199', 'HTA')

    Create Table #CurrentMonth (
    C_SSKey nvarchar(15),
    DS_SysStart datetime2,
    DS_SysEnd datetime2,
    ContractNumber nvarchar(7),
    Zone nvarchar(3)
    )

    insert into #CurrentMonth values('614-P085199-P', '2018-02-12 19:00:48.6300000', '9999-12-31 23:59:59.0000000', 'P085199', 'JRK')
    insert into #CurrentMonth values('614-P085453-P', '2018-02-28 19:00:46.6000000', '9999-12-31 23:59:59.0000000', 'P085453', 'PAT')

    Select * from #CurrentMonth
    Select * From #PriorMonth

    Any help would be greatly appreciated
    Thanks

    And based on the sample data what are you expecting as output?

  • LinksUp - Monday, March 5, 2018 9:21 AM

    ;with cte as
    (
     Select C_SSKey, MAX(DS_SysEnd) MaxDate
     from #PriorMonth
     group by C_SSKey
    )
    select c.* , cte.MaxDate
    from #CurrentMonth C
    inner join cte on cte.C_SSKey = c.C_SSKey

    Does this work? If not, please specify how you want the output to look given your test data.

    I'll try that, in the meantime,  I believe I solved my issue using Partition by and Row_Number

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

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