Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Compare previous field values using TSQL


Compare previous field values using TSQL

Author
Message
Hardy21
Hardy21
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1204 Visits: 1399
Hello,

I am using SQLServer 2008 R2. I have a requirement to display or highlight
which field has been modified in the next row compare to the previous row.

For example, my table & data is like:


declare @t table
(
id int,
boss_id int,
managername varchar(10),
title varchar(20),
changedate datetime,
changetime int
)

insert into @t values (1,25,'Matt','Manager','1/1/2010',40142);
insert into @t values (2,26,'Donald','Distrinct Manager','1/1/2010',40142)
insert into @t values (1,27,'Chris','Manager','1/1/2010',40145);
insert into @t values (2,25,'Matt','Manager','1/1/2010',40148);
insert into @t values (2,22,'John','Sales Counselor','1/1/2010',40148);
insert into @t values (1,24,'Donald 2','District Manager','1/1/2010',40155);

select * from @t order by id



There is a difference in boss_id, management and changetime fields for id = 1 and row numbers 1 & 2.
How to find that what are the fields that have been changed compare to previous row for perticular id?

Thanks in advance.

Thanks
sumit.agrawal
sumit.agrawal
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 220
Did u mean like if there are 3 rows for ID=1, and we are going to insert 1 more row for the same ID, then It should return all the fields where the changes have been done in the previous rows....

If it is, then you should consider to use cursors....
Praveen Goud Kotha
Praveen Goud Kotha
Old Hand
Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)

Group: General Forum Members
Points: 315 Visits: 93
I think..we can achieve without cursors also...

by fetching top 2 records and compare...

Praveen Goud
Hardy21
Hardy21
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1204 Visits: 1399
Can you please send me the query example?

Thanks
Praveen Goud Kotha
Praveen Goud Kotha
Old Hand
Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)

Group: General Forum Members
Points: 315 Visits: 93
Hi..

Here is my query..

select id, boss_id, managername, title, changedate, changetime
from
(
select id, boss_id, managername, title, changedate, changetime, row_number() over(partition by id order by boss_id desc) as bt
from @t
) A where A.bt in(1,2)

which is fetching the latest two records...which are going to be compared..
i hope ...the rest you can finish..

Praveen Goud
Hardy21
Hardy21
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1204 Visits: 1399
Hello,

I am able to resolve the issue.


select id, boss_id, managername, title, changedate, changetime, ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) rownumber
into #temptable
from @t order by id

select * from #temptable

select * from
(
select A.*,
case when A.managername <> B.managername then 1 else 0 end IsManagerChanged
, case when A.title <> B.title then 1 else 0 end IsTitleChanged
from #temptable A inner join
#temptable B on (A.rownumber = B.rownumber )
AND A.rownumber = 1
where a.id = b.id
UNION ALL
select A.*,
case when A.managername <> B.managername then 1 else 0 end IsManagerChanged
, case when A.title <> B.title then 1 else 0 end IsTitleChanged
from #temptable A inner join
#temptable B on (A.rownumber - 1 = B.rownumber )
where a.id = b.id
) A
order by a.id, a.rownumber



Result will be like:

(Result.JPG has been attached)


Thanks
Attachments
Result.JPG (13 views, 36.00 KB)
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