﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Administering / SQL Server 2005  / Comparing two rows / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 02:36:35 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Comparing two rows</title><link>http://www.sqlservercentral.com/Forums/Topic1415720-146-1.aspx</link><description>[quote][b]Johny.B (2/8/2013)[/b][hr]Another solution that uses left join and a [ARE_THE_SAME] column to mark the   columns having same values, you can modify the comparison logic  based on  what you want to achieve.The left outer join on a.sl&amp;lt;&amp;gt;b.sl have similar results to a cross join but does  not return the same matching rows. a cross join for the example would return 5*5=25 rows the outer returns 5*4=20, the 1-1,2-2,3-3,4-4,5-5 cases  does not returned from the join cause of the a.sl&amp;lt;&amp;gt;b.sl .the outer select is just for filter the cases that we wantselect * from(select case when a.isdn=b.isdn and a.msg=b.msg then 'YES' else 'NO' END as [ARE_THE_SAME],a.* from T1    aleft join T1 b  on a.sl&amp;lt;&amp;gt;b.sl) cwhere c.ARE_THE_SAME='YES'[/quote]You can accomplish the same thing with the subselect. Just do the filtering in your join.[code]select	'Yes' as ARE_THE_SAME,a.*from #T1 aleft join #T1 b on a.sl&amp;lt;&amp;gt;b.sl and a.isdn=b.isdn and a.msg=b.msgwhere b.sl is not null[/code]Not sure that is the way I would do that but it is simpler.Given the OP's comment [quote]Now I wanna compare whether there is any similar row comprising ISDN and MSG columns.[/quote]Maybe this is what they are after?[code]select ISDN, MSG, count(*) from #t1group by ISDN, MSGhaving count(*) &amp;gt; 1[/code]Who really knows? The questions and the follow up description just don't have much detail to work with.</description><pubDate>Fri, 08 Feb 2013 09:43:23 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Comparing two rows</title><link>http://www.sqlservercentral.com/Forums/Topic1415720-146-1.aspx</link><description>Another solution that uses left join and a [ARE_THE_SAME] column to mark the   columns having same values, you can modify the comparison logic  based on  what you want to achieve.The left outer join on a.sl&amp;lt;&amp;gt;b.sl have similar results to a cross join but does  not return the same matching rows. a cross join for the example would return 5*5=25 rows the outer returns 5*4=20, the 1-1,2-2,3-3,4-4,5-5 cases  does not returned from the join cause of the a.sl&amp;lt;&amp;gt;b.sl .the outer select is just for filter the cases that we wantselect * from(select case when a.isdn=b.isdn and a.msg=b.msg then 'YES' else 'NO' END as [ARE_THE_SAME],a.* from T1    aleft join T1 b  on a.sl&amp;lt;&amp;gt;b.sl) cwhere c.ARE_THE_SAME='YES'</description><pubDate>Fri, 08 Feb 2013 09:08:00 GMT</pubDate><dc:creator>Johny.B</dc:creator></item><item><title>RE: Comparing two rows</title><link>http://www.sqlservercentral.com/Forums/Topic1415720-146-1.aspx</link><description>Hi, The way I read it you are looking for not only duplicates but the original row the duplicates duplicated?If so try this ( I adjusted your code a tad):[code="sql"]create table #T1(SL int NOT NULL IDENTITY (1, 1),ISDN varchar(13) NULL,MSG varchar(50) NULL)insert into #T1(ISDN,MSG) values ('123','ABC');insert into #T1(ISDN,MSG) values ('123','ABC');insert into #T1(ISDN,MSG) values ('123','XYZ');insert into #T1(ISDN,MSG) values ('124','XYZ');insert into #T1(ISDN,MSG) values ('124','EFG');--extra duplicate added for better illustrationinsert into #T1(ISDN,MSG) values ('123','ABC');WITH CTE AS (select row_number() over(partition by isdn, msg order by isdn, msg) as instanceNumber,SL, ISDN, MSGfrom #t1)select lt.* from cte as lt inner join (select * from cte where instanceNumber=2) as rt	on lt.isdn=rt.isdn and lt.msg=rt.msg and lt.instanceNumber=1union allselect * from cte where instanceNumber&amp;gt;1drop table #t1[/code]</description><pubDate>Thu, 07 Feb 2013 16:29:07 GMT</pubDate><dc:creator>mmartin1</dc:creator></item><item><title>RE: Comparing two rows</title><link>http://www.sqlservercentral.com/Forums/Topic1415720-146-1.aspx</link><description>What is the goal? A set of rows with no duplicates? If so, would you want the row with the max ID? the min ID?Most importantly, what have you tried so far?</description><pubDate>Wed, 06 Feb 2013 09:50:41 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Comparing two rows</title><link>http://www.sqlservercentral.com/Forums/Topic1415720-146-1.aspx</link><description>Thank you, OPC.THREE[code="sql"]create table T1(SL int NOT NULL IDENTITY (1, 1),ISDN varchar(13) NULL,MSG varchar(50) NULL)insert into T1(ISDN,MSG) values ('123','ABC');insert into T1(ISDN,MSG) values ('123','ABC');insert into T1(ISDN,MSG) values ('123','XYZ');insert into T1(ISDN,MSG) values ('124','XYZ');insert into T1(ISDN,MSG) values ('124','EFG');[/code]Now I wanna compare whether there is any similar row comprising ISDN and MSG columns.</description><pubDate>Wed, 06 Feb 2013 03:16:14 GMT</pubDate><dc:creator>shohelr2003</dc:creator></item><item><title>RE: Comparing two rows</title><link>http://www.sqlservercentral.com/Forums/Topic1415720-146-1.aspx</link><description>T-SQL supports all your common comparison operators, e.g. = &amp;lt;&amp;gt;!= [i]or[/i] &amp;lt;&amp;gt;Comparisons involving two rows might imply the comparison would take place in a WHERE-clause. If you provide table definitions (CREATE TABLE statements), some test data (INSERT INTO statements) and a better idea of what you're trying to do I'd be happy to further help you.</description><pubDate>Tue, 05 Feb 2013 05:40:10 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>Comparing two rows</title><link>http://www.sqlservercentral.com/Forums/Topic1415720-146-1.aspx</link><description>Dear,I want to compare string type values between two rows within a table.Pls help me to do this.</description><pubDate>Tue, 05 Feb 2013 04:18:04 GMT</pubDate><dc:creator>shohelr2003</dc:creator></item></channel></rss>