Avatar of ttist25
ttist25
 asked on

Return IDs with a single value in another field

Working in SQL Server 2012.

I'm trying to find rows in my table where an ID exists with only 1 distinct value in another field:

My table looks something like this:
CREATE TABLE #tmpMyTable (
      MyID int NULL,
      MyField char NULL
      )
INSERT INTO #tmpMyTable
      VALUES      (1, 'A'),
                  (1, 'B'),
                  (1, 'B'),
                  (2, 'A'),
                  (3, 'A'),
                  (3, 'B'),
                  (3, 'B'),
                  (3, 'C'),
                  (4, 'A'),
                  (4, 'A'),
                  (5, 'D');

The query I'm trying to build would return the following:

2, A
4, A
5, D

I feel like this should be fairly simple but I'm failing miserably at it.  I've tried a SELF JOIN, GROUP BY, OVER PARTITION BY, etc. and I can't seem to get it.  

Any help will be greatly appreciated!

Thanks
Microsoft SQL Server

Avatar of undefined
Last Comment
ttist25

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ttist25

ASKER
Thanks again  Scott!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck