﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Compare one row with another in a Table ! / 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>Fri, 24 May 2013 01:01:14 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>[quote][b]drew.allen (1/9/2013)[/b][hr][quote][b]dwain.c (1/7/2013)[/b][hr]Any solution posed with a CTE can always be done without the CTE by making the CTE into a derived table as Lynn has shown.[/quote]That's not entirely true.  It's only true if the CTE is not recursive.  Recursive CTEs cannot simply be rewritten as derived tables.Even where they can be rewritten as derived tables, I find the CTE syntax easier to understand, and would recommend using the CTE in any case.Drew[/quote]Drew - 100% correct.  Can't understand why I didn't think of that considering the number of rCTEs I've posted to this site.</description><pubDate>Wed, 09 Jan 2013 17:45:55 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>[quote][b]dwain.c (1/7/2013)[/b][hr]Any solution posed with a CTE can always be done without the CTE by making the CTE into a derived table as Lynn has shown.[/quote]That's not entirely true.  It's only true if the CTE is not recursive.  Recursive CTEs cannot simply be rewritten as derived tables.Even where they can be rewritten as derived tables, I find the CTE syntax easier to understand, and would recommend using the CTE in any case.Drew</description><pubDate>Wed, 09 Jan 2013 08:41:48 GMT</pubDate><dc:creator>drew.allen</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>Agreed that not sure why you wouldn't want to use a CTE, but here's another alternative.[code="sql"]DECLARE @H TABLE    (House_Acc INT, Accountid INT, repcode VARCHAR(10))INSERT INTO @HSELECT 123, 1, 'J978A'UNION ALL SELECT 123, 2, 'J978A'UNION ALL SELECT 123, 3, 'J978A'UNION ALL SELECT 123, 4, 'EG567'UNION ALL SELECT 456, 21, 'BR5TG'UNION ALL SELECT 456, 22, 'BR5TG'UNION ALL SELECT 678, 66, 'ZHR06'UNION ALL SELECT 678, 45, 'ZHR06'UNION ALL SELECT 678, 34, 'NH678'SELECT a.House_Acc, b.AccountID, b.repcodeFROM (    SELECT House_Acc, AccountID, repcode        ,m=MAX(repcode) OVER (PARTITION BY House_Acc)    FROM @H) aINNER JOIN @H b    ON a.House_acc = b.House_accWHERE m &amp;lt;&amp;gt; a.repcode[/code]Any solution posed with a CTE can always be done without the CTE by making the CTE into a derived table as Lynn has shown.</description><pubDate>Mon, 07 Jan 2013 02:20:43 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>Thank You , Everyone for the Help !</description><pubDate>Sat, 05 Jan 2013 22:19:45 GMT</pubDate><dc:creator>nitin_456</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>[quote][b]nitin_456 (1/5/2013)[/b][hr]Is there any other way to achieve this without CTE.[/quote]Not sure why you don't want the cte version, but this works (though untested):[code="sql"]SELECT A.House_acc ,        A.AccountId ,        A.repcode  FROM    (        SELECT House_acc        FROM  yourtable        GROUP BY House_acc        HAVING (COUNT(DISTINCT repcode) &amp;gt; 1)    ) dt     INNER JOIN yourtable AS A        ON dt.House_acc = A.House_acc;[/code]</description><pubDate>Sat, 05 Jan 2013 22:08:23 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>Thanks Celko, I will keep your advice in my mind !</description><pubDate>Sat, 05 Jan 2013 17:17:52 GMT</pubDate><dc:creator>nitin_456</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. This is minimal polite behavior on SQL forums. Think about how rude it is to make us re-type your data and guess at your DDL to answer your questions for free. When you are at a restaurant, do you put a bad tip in the dirty dishes just make the waiter fish for it? NO! The why do you do it here? We do not even have the name of the table! Here is my guess at what you should have done. Was I right? CREATE TABLE Households(house_id INTEGER NOT NULL, acct_nbr INTEGER NOT NULL,  PRIMARY KEY (house_id, acct_nbr) rep_code CHAR(5) NOT NULL);INSERT INTO Households VALUES(123, 1, 'J978A'),(123, 2, 'J978A'),(123, 3, 'J978A'),(123, 4, 'EG567'),(456, 21, 'BR5TG'),(456, 22, 'BR5TG'),(678, 66, 'ZHR06'),(678, 45, 'ZHR06'),(678, 34, 'NH678');Why was that so hard you could not be bothered to do it? [quote] If there is a different rep_code in a same house_id, I want all the records [sic] for that house_id. [/quote] Rows are nothing like records. Again, you are missing some fundamental concepts. SELECT house_id  FROM Households  GROUP BY house_idHAVING MIN(rep_code) &amp;lt; MAX(rep_code);You have just been blasted by Celko; it is a Rite of Passage in the SQL world. Get over it and learn. </description><pubDate>Sat, 05 Jan 2013 17:06:15 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>Is there any other way to achieve this without CTE.</description><pubDate>Sat, 05 Jan 2013 16:25:50 GMT</pubDate><dc:creator>nitin_456</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>untested...[code="sql"];with cte as(SELECT House_accFROM  yourtableGROUP BY House_accHAVING (COUNT(DISTINCT repcode) &amp;gt; 1))SELECT A.House_acc ,        A.AccountId ,        A.repcode  FROM       cte INNER JOIN yourtable AS A ON cte.House_acc = A.House_acc;[/code]</description><pubDate>Sat, 05 Jan 2013 15:50:52 GMT</pubDate><dc:creator>J Livingston SQL</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>I want 678 too in my report.</description><pubDate>Sat, 05 Jan 2013 15:30:34 GMT</pubDate><dc:creator>nitin_456</dc:creator></item><item><title>RE: Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>[quote][b]nitin_456 (1/5/2013)[/b][hr]Hi ,  House_Acc	Accountid	repcode123	           1	         J978A123	           2	         J978A123	           3	         J978A123	           4	         EG567456	          21	         BR5TG456	          22	         BR5TG678	          66	         ZHR06678	          45	         ZHR06678	          34	         NH678How Can I compare REPCODE in the same House_Acc, If there is a different repcode in a same house_acc , I want all the records for that house_acc.For ex. so my output will be In House_acc 123 ,one of the rep code is different . In output , I want all the four records123	           1	         J978A123	           2	         J978A123	           3	         J978A123	           4	         EG567If all the accounts in House_acc has same repcode then ignore that.Thanks,Nick[/quote]why dont you want House_acc = 678 in the result set?</description><pubDate>Sat, 05 Jan 2013 15:27:59 GMT</pubDate><dc:creator>J Livingston SQL</dc:creator></item><item><title>Compare one row with another in a Table !</title><link>http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx</link><description>Hi ,  House_Acc	Accountid	repcode123	           1	         J978A123	           2	         J978A123	           3	         J978A123	           4	         EG567456	          21	         BR5TG456	          22	         BR5TG678	          66	         ZHR06678	          45	         ZHR06678	          34	         NH678How Can I compare REPCODE in the same House_Acc, If there is a different repcode in a same house_acc , I want all the records for that house_acc.For ex. so my output will be a) In House_acc 123 ,one of the rep code is different . In output , I want all the four records123	           1	         J978A123	           2	         J978A123	           3	         J978A123	           4	         EG567678	          45	         ZHR06678	          34	         NH678b) If all the accounts in House_acc has same repcode then ignore that.456	          21	         BR5TG456	          22	         BR5TGThanks,Nick</description><pubDate>Sat, 05 Jan 2013 15:19:21 GMT</pubDate><dc:creator>nitin_456</dc:creator></item></channel></rss>