Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 compare two rows of same table

Author  Topic 

karthick.amace
Starting Member

23 Posts

Posted - 2010-08-11 : 22:17:10
Hi bros,

I need to compare the next row with the previous row of same table and produce nonidentical column.
for eg... say
mytable has

Row 1 => karthick 120 150 180
Row 2 => karthick 150 150 180

it want to gve output 150 of second row.
Plz suggest me...

- Karthick

------------------------------------------------------
"Desire makes what you wants to do"

Sachin.Nand

2937 Posts

Posted - 2010-08-12 : 00:12:46
Do you have any identical column?
Just post more sample data as it would be more clear on what you exactly want.


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-12 : 11:27:16
sounds like

SELECT other columns except rn
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Field1 DESC) AS rn,*
FROM Table
)t
WHERE rn=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

revdnrdy
Posting Yak Master

220 Posts

Posted - 2010-08-12 : 11:44:13
quote:
Originally posted by karthick.amace

Hi bros,

I need to compare the next row with the previous row of same table and produce nonidentical column.
for eg... say
mytable has

Row 1 => karthick 120 150 180
Row 2 => karthick 150 150 180

it want to gve output 150 of second row.
Plz suggest me...


Is there a reason you couldn't just use a self join? Then you can compare rows to see which one meets your criteria (max value I guess since 150 is what you want in your example).


SELECT DISTINCT Column1, Column2  
FROM MyTable A,
MyTable B
WHERE A.id = B.id
A.Value > b.Value


Something like that? I don't understand what 'produce non-identical column' means but you can define that in the SELECT.

r&r

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-12 : 12:28:05
quote:
Originally posted by revdnrdy

quote:
Originally posted by karthick.amace

Hi bros,

I need to compare the next row with the previous row of same table and produce nonidentical column.
for eg... say
mytable has

Row 1 => karthick 120 150 180
Row 2 => karthick 150 150 180

it want to gve output 150 of second row.
Plz suggest me...


Is there a reason you couldn't just use a self join? Then you can compare rows to see which one meets your criteria (max value I guess since 150 is what you want in your example).


SELECT DISTINCT Column1, Column2  
FROM MyTable A,
MyTable B
WHERE A.id = B.id
A.Value > b.Value


Something like that? I don't understand what 'produce non-identical column' means but you can define that in the SELECT.

r&r




see

http://www.sqlservercentral.com/articles/T-SQL/61539/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -