Compare one row with another in a Table !

  • Hi ,

    House_AccAccountidrepcode

    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

  • nitin_456 (1/5/2013)


    Hi ,

    House_AccAccountidrepcode

    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

  • I want 678 too in my report.

  • 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

  • Is there any other way to achieve this without CTE.

  • Thanks Celko, I will keep your advice in my mind !

  • 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;

  • Thank You , Everyone for the Help !

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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

  • drew.allen (1/9/2013)


    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

    Drew - 100% correct. Can't understand why I didn't think of that considering the number of rCTEs I've posted to this site.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply