Share via

T-SQL question

chuck DM 101 Reputation points
2020-09-11T05:57:29.01+00:00

I have the following table:-
ID Name
01 AA
02 AB
01 AC

I need to get all the data where Name =’AA’ and corresponding all the IDs.
Say for example, ID (01) has Name =’AA’. ID (01) has also Name =’AC’. SO the out come would be:-

ID Name
01 AA
01 AC

How to get it? Any help would be highly appreciated.
Thanks

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


5 answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,626 Reputation points
    2020-09-14T00:55:32.36+00:00

    Hi chuckDM-9763

    Do you have any updates?
    Please remember to accept the answers if they helped. Your action would be helpful to other users who encounter the same issue and read this thread.
    Thank you for understanding!

    Echo

    Was this answer helpful?

    0 comments No comments

  2. Jingyang Li 1 Reputation point
    2020-09-11T14:13:19.48+00:00
     select t1.ID,t1.Name from test t1
     where exists (select 1 from test t2
     where t1.id=t2.id and t2.name='AA')
    

    Was this answer helpful?

    0 comments No comments

  3. Tom Cooper 8,501 Reputation points
    2020-09-11T08:08:39.567+00:00
    Select t2.ID, t2.Name
    From YourTableName t1
    Inner Join YourTableName t2 On t1.ID = t2.ID
    Where t1.Name = 'AA';
    

    Tom

    Was this answer helpful?

    0 comments No comments

  4. EchoLiu-MSFT 14,626 Reputation points
    2020-09-11T08:06:34.297+00:00

    Please refer to :

        create table test (ID int,Name char(15))  
        insert into test values  
        (01,'AA'),  
        (02,'AB'),  
        (01,'AC')  
      
    select * from test  
    where name='AA' or id=(select id from test where name='AA')  
      
    drop table test   
    

    23909-image.png

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Best Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


  5. Olaf Helper 47,621 Reputation points
    2020-09-11T08:02:47.437+00:00

    This should work for you:

    SELECT *
    FROM yourTable AS T
    WHERE T.ID IN (SELECT SUB.ID FROM yourTable AS SUB WHERE SUB.Name = 'AA')
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.