Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Compare one row with another in a Table ! Expand / Collapse
Author
Message
Posted Saturday, January 5, 2013 3:19 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 25, 2014 12:40 PM
Points: 22, Visits: 121
Hi ,


House_Acc Accountid repcode
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

How 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 records

123 1 J978A
123 2 J978A
123 3 J978A
123 4 EG567

678 45 ZHR06
678 34 NH678


b) If all the accounts in House_acc has same repcode then ignore that.
456 21 BR5TG
456 22 BR5TG


Thanks,
Nick
Post #1403281
Posted Saturday, January 5, 2013 3:27 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:11 AM
Points: 1,900, Visits: 18,985
nitin_456 (1/5/2013)
Hi ,


House_Acc Accountid repcode
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

How 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 records

123 1 J978A
123 2 J978A
123 3 J978A
123 4 EG567


If all the accounts in House_acc has same repcode then ignore that.

Thanks,
Nick


why dont you want House_acc = 678 in the result set?


__________________________________________________________________
you can lead a user to data....but you cannot make them think !
__________________________________________________________________
Post #1403284
Posted Saturday, January 5, 2013 3:30 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 25, 2014 12:40 PM
Points: 22, Visits: 121
I want 678 too in my report.
Post #1403287
Posted Saturday, January 5, 2013 3:50 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:11 AM
Points: 1,900, Visits: 18,985
untested...


;with cte as
(
SELECT House_acc
FROM yourtable
GROUP BY House_acc
HAVING (COUNT(DISTINCT repcode) > 1)
)


SELECT A.House_acc ,
A.AccountId ,
A.repcode
FROM
cte INNER JOIN yourtable AS A ON cte.House_acc = A.House_acc;




__________________________________________________________________
you can lead a user to data....but you cannot make them think !
__________________________________________________________________
Post #1403288
Posted Saturday, January 5, 2013 4:25 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 25, 2014 12:40 PM
Points: 22, Visits: 121
Is there any other way to achieve this without CTE.
Post #1403295
Posted Saturday, January 5, 2013 5:06 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 6:40 AM
Points: 1,945, Visits: 2,906
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?

If there is a different rep_code in a same house_id, I want all the records [sic] for that house_id.


Rows are nothing like records. Again, you are missing some fundamental concepts.

SELECT house_id
FROM Households
GROUP BY house_id
HAVING MIN(rep_code) < 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.




Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1403298
Posted Saturday, January 5, 2013 5:17 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 25, 2014 12:40 PM
Points: 22, Visits: 121
Thanks Celko, I will keep your advice in my mind !
Post #1403299
Posted Saturday, January 5, 2013 10:08 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:09 PM
Points: 23,270, Visits: 31,985
nitin_456 (1/5/2013)
Is there any other way to achieve this without CTE.


Not sure why you don't want the cte version, but this works (though untested):


SELECT A.House_acc ,
A.AccountId ,
A.repcode
FROM
(
SELECT House_acc
FROM yourtable
GROUP BY House_acc
HAVING (COUNT(DISTINCT repcode) > 1)
) dt
INNER JOIN yourtable AS A
ON dt.House_acc = A.House_acc;





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1403305
Posted Saturday, January 5, 2013 10:19 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 25, 2014 12:40 PM
Points: 22, Visits: 121
Thank You , Everyone for the Help !
Post #1403306
Posted Monday, January 7, 2013 2:20 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:00 PM
Points: 3,627, Visits: 5,267
Agreed that not sure why you wouldn't want to use a CTE, but here's another alternative.

DECLARE @H TABLE
(House_Acc INT, Accountid INT, repcode VARCHAR(10))

INSERT INTO @H
SELECT 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.repcode
FROM (
SELECT House_Acc, AccountID, repcode
,m=MAX(repcode) OVER (PARTITION BY House_Acc)
FROM @H) a
INNER JOIN @H b
ON a.House_acc = b.House_acc
WHERE m <> a.repcode


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.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1403490
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse