SQLServerCentral.com / SQL Server 2008 / T-SQL (SS2K8) / Compare one row with another in a Table ! / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comTue, 03 Mar 2015 15:51:10 GMT20RE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx[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.Wed, 09 Jan 2013 17:45:55 GMTdwain.cRE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx[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.DrewWed, 09 Jan 2013 08:41:48 GMTdrew.allenRE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspxAgreed 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 &lt;&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.Mon, 07 Jan 2013 02:20:43 GMTdwain.cRE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspxThank You , Everyone for the Help !Sat, 05 Jan 2013 22:19:45 GMTnitin_456RE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx[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) &gt; 1) ) dt INNER JOIN yourtable AS A ON dt.House_acc = A.House_acc;[/code]Sat, 05 Jan 2013 22:08:23 GMTLynn PettisRE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspxThanks Celko, I will keep your advice in my mind !Sat, 05 Jan 2013 17:17:52 GMTnitin_456RE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspxPlease 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) &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. Sat, 05 Jan 2013 17:06:15 GMTCELKORE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspxIs there any other way to achieve this without CTE.Sat, 05 Jan 2013 16:25:50 GMTnitin_456RE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspxuntested...[code="sql"];with cte as(SELECT House_accFROM yourtableGROUP BY House_accHAVING (COUNT(DISTINCT repcode) &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]Sat, 05 Jan 2013 15:50:52 GMTJ Livingston SQLRE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspxI want 678 too in my report.Sat, 05 Jan 2013 15:30:34 GMTnitin_456RE: Compare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspx[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?Sat, 05 Jan 2013 15:27:59 GMTJ Livingston SQLCompare one row with another in a Table !http://www.sqlservercentral.com/Forums/Topic1403281-392-1.aspxHi , 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,NickSat, 05 Jan 2013 15:19:21 GMTnitin_456