select query

Spunny 366 Reputation points
2025-09-05T17:23:44.97+00:00

declare @tmp table

(

id INT IDENTITY(1,1),

Summary varchar(500),

type varchar(10),

filename varchar(500)

)

INSERT INTO @tmp

select 'xxx', 'PDF', 'FileName1'

union all

select 'yyy', 'PDF', 'FileName2'

union all

Select 'yyy', 'MSG', 'filename3'

union all

select 'zzz', 'PDF', 'filename4'

union all

select 'zzz', 'PDF', 'filename5'

select * from @tmp

I need to get a result of id 1, 4, 5

if there is any summary that has msg record, that shouldn't be output
TIA

SQL Server | SQL Server Transact-SQL
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 127.7K Reputation points MVP Volunteer Moderator
    2025-09-05T21:47:36.43+00:00
    select * 
    from   @tmp a
    WHERE  NOT EXISTS (SELECT * 
                       FROM   @tmp b
                       WHERE  b.Summary = a.Summary
                         AND  b.type = 'MSG')
    

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.