Unique records

  • Hi Team,

    I have a 'admins' table with below records

    groupname

    --------------

    TNR Unify

    TJBICH

    TJKICH

    TJAICH

    : Result should be : None

    if name is similar to all 4 groups then uniqe name should be displayed, else none should be display.

    groupname

    --------------

    TJBICH

    TJKICH

    TJAICH

    : Result should be : ICH

    using below query

    select name from admins where group in ('TJA','tJB','tJK','tnr')

    group by name having count(*)>1

    Please help

  • You've been here long enough to know that you need to post DDL, consumable data and desired results to get a working solution.

    See the first link in my signature for details.


  • Try

    select name

    from admins

    where group in ('TJA','tJB','tJK','tnr')

    group by name

    having count( distinct group)=4

  • Phil Parkin (3/31/2015)


    You've been here long enough to know that you need to post DDL, consumable data and desired results to get a working solution.

    See the first link in my signature for details.

    100% in agreement with that, but I'm feeling generous this morning.

    WITH admins ([group], name) AS

    (

    SELECT 'TJB','ICH'

    UNION ALL SELECT 'TJK','ICH'

    UNION ALL SELECT 'TJA','ICH'

    --UNION ALL SELECT 'TNR','Unify'

    )

    SELECT name=MIN(name)

    FROM admins

    WHERE [group] in ('TJA','tJB','tJK','TNR')

    HAVING COUNT(DISTINCT name)=1;


    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 4 posts - 1 through 4 (of 4 total)

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