SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
Stephen crocker
Stephen crocker
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2034 Visits: 466
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
LinksUp
LinksUp
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5280 Visits: 5579
;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/
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (385K reputation)SSC Guru (385K reputation)SSC Guru (385K reputation)SSC Guru (385K reputation)SSC Guru (385K reputation)SSC Guru (385K reputation)SSC Guru (385K reputation)SSC Guru (385K reputation)

Group: General Forum Members
Points: 385821 Visits: 42551
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?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Stephen crocker
Stephen crocker
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2034 Visits: 466
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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search