Question

I have read the MSDN / Technet and understand there should be a column from the outer select inside the sub-select to join on, but with my table structure that is not exactly what I am after. I have a table with 2 columns, say Name and Type. There may be duplicate entries of Name, and duplicate entries of Type, but not a duplicate record of both Name and Type (think like a unique key across these 2 columns).

Sample fiddle: http://sqlfiddle.com/#!3/95f3b/3

Sample code:

DECLARE @food AS TABLE (FoodName NVARCHAR(200), FoodType NVARCHAR(200))
DECLARE @NEWfoods AS TABLE (FoodName NVARCHAR(200), FoodType NVARCHAR(200))

INSERT INTO @food (FoodName, FoodType) VALUES 
 ('Apples', 'Fruit')
,('Avocado','Fruit')
,('Bananas', 'Fruit')
,('Mangos', 'Fruit')
,('Bread', 'Grain')
,('Cottage Cheese', 'Dairy')
,('Tacos', 'Meals')
,('Carrots', 'Vegetables')
,('Celery', 'Vegatables')

INSERT INTO @NEWfoods ( FoodName, FoodType ) VALUES  
 ('Avocado','Vegetables')
,('Apples','Fruit')
,('Salt','Preservative')
,('Turkey','Protein')
,('Bread','Grain')
,('Bread','Grain')
,('Tacos','Meals')


SELECT
     f.FoodName
    ,f.FoodType 
FROM @food AS f
WHERE NOT EXISTS (
    SELECT * FROM @NEWfoods
    )

What I expect to happen is Avocado:Vegetables will get inserted since the base table contains Avocado:Fruit but not Avocado:Vegetables (keeping in mind I am using both columns as a unique key), Bread:Grain and Tacos:Meals will not be inserted at all since they already exist in the base table, and that Salt:Preservative will be inserted, too.

So at the end of all of this it seems I am not using the right function here - that NOT EXISTS in itself is not the right way. Coder's block! Any help would be appreciated. :)

Was it helpful?

Solution

Try this.

   SELECT
         f.FoodName
        ,f.FoodType 
    FROM @food AS f
    WHERE NOT EXISTS (
        SELECT * FROM @NEWfoods
        where FoodName = f.FoodName
        AND FoodType = f.FoodType
        )

OTHER TIPS

DECLARE @food AS TABLE (FoodName NVARCHAR(200), FoodType NVARCHAR(200))
DECLARE @NEWfoods AS TABLE (FoodName NVARCHAR(200), FoodType NVARCHAR(200))

INSERT INTO @food (FoodName, FoodType) VALUES 
 ('Apples', 'Fruit')
,('Avocado','Fruit')
,('Bananas', 'Fruit')
,('Mangos', 'Fruit')
,('Bread', 'Grain')
,('Cottage Cheese', 'Dairy')
,('Tacos', 'Meals')
,('Carrots', 'Vegetables')
,('Celery', 'Vegatables')

INSERT INTO @NEWfoods ( FoodName, FoodType ) VALUES  
 ('Avocado','Vegetables')
,('Apples','Fruit')
,('Salt','Preservative')
,('Turkey','Protein')
,('Bread','Grain')
,('Bread','Grain')
,('Tacos','Meals')


SELECT
     f.FoodName
    ,f.FoodType 
FROM @food AS f
WHERE f.FoodType  NOT IN (
    SELECT N.FoodType FROM @NEWfoods N
    WHERE f.FoodName = n.FoodName AND F.FoodType = n.FoodType
    )
   **--- EITHER THS WAY** 
    select DISTINCT FF.FoodName,FF.FoodType from 
    (select f.FoodName,f.FoodType from @food f
     )AS FF LEFT JOIN @NEWfoods N ON N.FoodName = FF.FoodName AND N.FoodType = FF.FoodType
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top