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


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:10 AM
Points: 2,085, Visits: 22,081
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
and remember....every day is a school day
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


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:10 AM
Points: 2,085, Visits: 22,081
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
and remember....every day is a school day
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: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 @ 10:01 PM
Points: 21,773, Visits: 34,449
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 @ 9:50 PM
Points: 3,963, Visits: 6,292
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1403490
Posted Wednesday, January 9, 2013 8:41 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:01 PM
Points: 1,293, Visits: 5,620
dwain.c (1/7/2013)

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.


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


J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Post #1404836
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse