0

How can I query a Microsoft SQL table to return only an InstNum that has all Profile type 'C' or 'U'?

Please see my table structure below.

I tried :

Select InstNum from Table Where Profile = 'C' Group By InstName;

RESULT : 001, 002, 003 EXPECTING : 003 (Because all Profiles in 003 is 'C')

InstNum Profile
001 U
001 C
001 U
002 U
002 U
002 U
003 C
003 C
003 C

Any help is really appreciated!

1
  • Why not 002 ? Isn't your condition return only an InstNum that has all Profile type 'C' or 'U' ?
    – Squirrel
    May 12 at 2:12

2 Answers 2

1

Try using an EXCEPT…

SELECT InstNum
FROM Table
WHERE Profile = 'C'

EXCEPT

SELECT InstNum
FROM Table
WHERE Profile <> ‘C'
GROUP BY InstNum;
0
0
WITH sameProfiles AS
  (
    SELECT
      COUNT([instNum]) cnt
      , [instNum]
    FROM
      (
        SELECT distinct
          [instNum]
          , [Profile]
        FROM
          myTable1 ) q
    GROUP BY
      [instNum]
    HAVING
      COUNT([instNum]) = 1)
SELECT DISTINCT
  [instnum]
FROM
  myTable1
WHERE
  [profile] = 'C'
and [instNum] in
  (
    SELECT
      [instNum]
    FROM
      sameProfiles);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.