Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Compare one row with another in a Table !


Compare one row with another in a Table !

Author
Message
nitin_456
nitin_456
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 129
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
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3447 Visits: 33026
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

nitin_456
nitin_456
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 129
I want 678 too in my report.
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3447 Visits: 33026
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

nitin_456
nitin_456
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 129
Is there any other way to achieve this without CTE.
nitin_456
nitin_456
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 129
Thanks Celko, I will keep your advice in my mind !
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24177 Visits: 37948
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;




Cool
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)
nitin_456
nitin_456
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 129
Thank You , Everyone for the Help !
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search