Want to get Correct Id

  • Tablename Consumer:

    ID Resource

    119319004 RG

    119319004 MI

    119319005 RG

    119319005 MI

    119319005 BT

    119319006 RG

    I want id whose Resource is only RG and MI that's(119319004)

    Output

    119319004

    Please reply..

  • Based on what you said:-

    SELECT

    ID

    FROM

    dbo.[Consumer]

    WHERE

    ID = 119319004

    AND

    Resource = 'RG'

  • That dataset was just an example.I need only those id which has MI and RG both

  • How about:-

    SELECT

    ID

    FROM

    dbo.[Consumer]

    WHERE

    Resource in ('RG','MI')

  • This query will return those id also which contain resource type BT (ex: 119319005).I only want those ids which contain RG and MI only.

  • atultiwari.eng06 (9/17/2014)


    Tablename Consumer:

    ID Resource

    119319004 RG

    119319004 MI

    119319005 RG

    119319005 MI

    119319005 BT

    119319006 RG

    I want id whose Resource is only RG and MI that's(119319004)

    Output

    119319004

    Please reply..

    Could you do something like:

    with cte1 as (select id, count(*) from mtable where mtable.resource in ('rg', 'mi')

    group by id having count(*) = 2)

    select id

    from mtable

    inner join cte1 on mtable.id = cte1.id ;

    I know this may or may not work for you as written but maybe it will give you an idea.

  • Ah, I see. How about:-

    SELECT C1.ID, C1.[Resource]

    FROM dbo.[Consumer] C1

    WHERE NOT EXISTS

    (SELECT ID

    FROM dbo.[Consumer] C2

    WHERE C2.[Resource] NOT IN ('MI','RG')

    AND C2.ID = C1.ID)

    AND EXISTS

    (SELECT ID

    FROM dbo.[Consumer] C3

    WHERE C3.[Resource] = 'RG'

    AND C3.ID = C1.ID)

    AND EXISTS

    (SELECT ID

    FROM dbo.[Consumer] C4

    WHERE C4.[Resource] = 'MI'

    AND C4.ID = C1.ID)

  • select c.ID,count(*) [Cnt]

    from dbo.Consumer c

    where c.Resource in ('RG','MI') and c.ID=119319004

    group by c.ID

    having count(*)=2

    Igor Micev,My blog: www.igormicev.com

  • Please see my next reply.

  • You should read the discussion on this article. Of course, you must change the conditions a little bit for "C".

    http://www.sqlservercentral.com/articles/T-SQL/88244/

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (9/17/2014)


    You should read the discussion on this article. Of course, you must change the conditions a little bit for "C".

    http://www.sqlservercentral.com/articles/T-SQL/88244/

    Thank you Luis - for the clue.

    Based on the article Find Customers Who Bought "A" and "B" But Not "C" (SQL Spackle)[/url]

    SELECT

    ID

    FROM dbo.Consumer

    WHERE Resource IN ('RG','MI')

    GROUP BY ID

    HAVING COUNT(DISTINCT Resource) =2

    EXCEPT

    -- Ignore all Resource except RG and MI

    SELECT ID

    FROM dbo.Consumer

    WHERE Resource NOT IN ('RG','MI');

  • Thanks bro u nailed it

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

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