samedi 27 juin 2015

How can I use a string list from a select statement in a NOT IN clause

I want to build a list of Lastnames to filter out from the results of the second select statement something like this (which doesn't do it)

DECLARE @ExcludedList VARCHAR(MAX)
SET @ExcludedList = (SELECT  TOP 20 Lastname
FROM dbo.Subscribers
where [Firstname] = 'Dave')

SELECT [Firstname], Count([Firstname]) as [Count] from Subscribers
WHERE [Lastname] not in (@ExcludedList) 

Aucun commentaire:

Enregistrer un commentaire