Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Comparing Current and Previous record values based on compound identifier

$
0
0

I am trying to compare the current and previous record values of a set of records based on a compound identifier. This is a learning thing. I am just snapshotting configuration data into the Configuration table on specified date/times and having a parent journal entry that maintains the entrytype and date. I want to report out the data ONLY if a configuration change was made between snapshots.

i.e. if I snapshot as follows:

12-1-2013 - 70 records, journal ID = 1 (say 'allow updates' = 0, 'fill factor (%)' = 50)

12-2-2013 - 70 records, journal Id = 2 (say 'allow updates' = 0, 'fill factor (%)' = 70)

12-3-2013 - 70 records, journal Id = 2 (say 'allow updates' = 1, 'fill factor (%)' = 50)

12-4-2013 - 70 records, journal Id = 2 (say 'allow updates' = 1, 'fill factor (%)' = 80)

12-5-2013 - 70 records, journal Id = 2 (say 'allow updates' = 0, 'fill factor (%)' = 80)

 

It should list:

12-2-2013 Fill Factor (%), 50, 70

12-3-2013 Allow Updates, 0, 1

                Fill Factor (%), 70, 50

12-4-2013 Fill Factor (%), 50, 80

12-5-2013 Allow Updates, 1, 0

 

I was trying to use a CTE, but I am stuck on the compound value due to many rows being included per JournalID.

 
    ;with cteConfiguration as (
      select j.JournalID
            ,o.ConfigurationID
            ,o.Configuration
            ,o.Value
            ,o.ValueInUse 
            ,j.EntryDate
            ,row_number() over (order by JournalID, ConfigurationID) AS Record
        from Baseline.Configuration o inner join Baseline.Journal j
                                              on j.JournalID = o.JournalID
                                      inner join Baseline.EntryType e
                                              on e.EntryTypeID = j.EntryTypeID
                                             and e.EntryType = 'Configuration Snapshot'
      where j.EntryDate >= @ldt_BeginDate
    )
    select j.EntryDate
          ,o.Configuration
          ,o.Value
          ,n.Value 
      from cteConfiguration o left join cteConfiguration n

Here are the table structures.

create schema Baseline authorization dbo
go
create table Baseline.EntryType(EntryTypeID int identity(1,1) not null,
	                              EntryType nvarchar(128) not null,
	                              CreatedBy nvarchar(128) not null default original_login(),
	                              CreatedOn datetime not null default getdate(),
 constraint [pk__Baseline__EntryType__EntryTypeID] primary key nonclustered (EntryTypeID asc)
 with (pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on) on [PRIMARY]
) on [PRIMARY]
go
create clustered index cidx__Baseline__EntryType__EntryType on Baseline.EntryType (EntryType)
go
create table Baseline.Journal(JournalID bigint identity(1,1) not null,
	                            EntryTypeID int not null,
	                            EntryDate datetime not null default getdate(),    
	                            CreatedBy nvarchar(128) not null default original_login(),
	                            CreatedOn datetime not null default getdate(),
 constraint [pk__Baseline__Journal__JournalID] primary key nonclustered (JournalID asc)
 with (pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on) on [PRIMARY]
) on [PRIMARY]
go
create clustered index cidx__Baseline__Journal__EntryDate__EntryTypeID__JournalID on Baseline.Journal (EntryDate, EntryTypeID, JournalID)
go
alter table Baseline.Journal  with check add constraint fk__Baseline__Journal__EntryTypeID__to__Baseline__EntryType__EntryTypeID foreign key(EntryTypeID)
references Baseline.EntryType (EntryTypeID)
go
alter table Baseline.Journal check constraint fk__Baseline__Journal__EntryTypeID__to__Baseline__EntryType__EntryTypeID
go
create table Baseline.Configuration(ConfigurationID int not null,
                                    JournalID bigint not null,
                                    Configuration nvarchar(128) not null,
                                    Value sql_variant null,
                                    ValueInUse sql_variant null,
                                    CreatedBy nvarchar(128) not null default original_login(),
                                    CreatedOn datetime not null default getdate(),
 constraint [pk__Baseline__Configuration__ConfigurationID__JournalID] primary key nonclustered (ConfigurationID asc, JournalID asc)
 with (pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on) on [PRIMARY]
) on [PRIMARY]
go
create clustered index cidx__Baseline__Configuration__JournalID__ConfigurationID on Baseline.Configuration (JournalID, ConfigurationID)
go
alter table Baseline.Configuration  with check add constraint fk__Baseline__Configuration__JournalID__to__Baseline__Journal__JournalID foreign key(JournalID)
references Baseline.Journal (JournalID)
go
alter table Baseline.Configuration check constraint fk__Baseline__Configuration__JournalID__to__Baseline__Journal__JournalID
go

-- 
insert into Baseline.EntryType (EntryType)
     values ('Configuration Snapshot')
     


John M. Couch



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>