color exist or not in sql server

tani oju 1 Reputation point
2021-01-06T07:50:23.51+00:00

HI i have one doubt in sql server .
how to find expeted colors exist or not if not exits
then get productid informaton

table : productinfo
CREATE TABLE [dbo].[productInfo](
[Productid] [int] NULL,
[Productcolor] varchar NULL
)

INSERT [dbo].[productInfo] ([Productid], [Productcolor]) VALUES (1, N'red')
INSERT [dbo].[productInfo] ([Productid], [Productcolor]) VALUES (1, N'blue')
INSERT [dbo].[productInfo] ([Productid], [Productcolor]) VALUES (1, N'white')
INSERT [dbo].[productInfo] ([Productid], [Productcolor]) VALUES (2, N'red')
INSERT [dbo].[productInfo] ([Productid], [Productcolor]) VALUES (2, N'blue')
INSERT [dbo].[productInfo] ([Productid], [Productcolor]) VALUES (3, N'red')
INSERT [dbo].[productInfo] ([Productid], [Productcolor]) VALUES (4, N'red')
INSERT [dbo].[productInfo] ([Productid], [Productcolor]) VALUES (4, N'blue')
INSERT [dbo].[productInfo] ([Productid], [Productcolor]) VALUES (4, N'white')

based on above data I want output like below
Productid | Productcolor
2 |white missed
3 |blue missed
3 |white missed

each productid should have red,blue,white colors .if any colore not exist then
need to display that productid and color.if all colors exist then no need to display productid

I tried like below

select * from productinfo where Productcolor not in ( 'red','blue','white')

could you please tell me how to write a query to achive this task in sql server .

Developer technologies Transact-SQL
SQL Server Other
{count} votes

4 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-01-06T08:44:06.687+00:00

    Check this query:

    ; 
    with MC as
    (
        select Productid, c as ProductColor
        from productInfo
        cross apply (values ('red'), ('blue'), ('white')) t(c)
        except
        select Productid, ProductColor
        from productInfo
    )
    select ProductId, ProductColor + ' missed' as ProductColor
    from MC
    order by ProductId, ProductColor
    
    0 comments No comments

  2. MelissaMa-MSFT 24,221 Reputation points
    2021-01-06T09:01:46.54+00:00

    Hi @tani oju ,

    Thank you so much for posting here in Microsoft Q&A.

    Please also refer below:

    ;with cte as (  
    SELECT distinct [Productid],b.color  
    FROM    productinfo a  
    CROSS JOIN (values ('red'), ('blue'), ('white')) b(color))  
    select Productid,color+ ' missed' ProductColor   
    from cte a where not exists   
    (select 1 from productinfo b where b.Productid=a.Productid and a.color=b.Productcolor)  
    

    Output:

    Productid	ProductColor  
    2	white missed  
    3	blue missed  
    3	white missed  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. MelissaMa-MSFT 24,221 Reputation points
    2021-01-06T09:12:39.29+00:00

    Hi @tani oju ,

    Please also refer another method from below:

    ;WITH base (color) as (  
        SELECT 'red' UNION ALL  
        SELECT 'blue' UNION ALL  
        SELECT 'white'  
    ),  
    Product as (  
        SELECT DISTINCT [Productid]  
        FROM productinfo t      
    ),  
    pairs as (   
        SELECT *  
        FROM Product, base  
    )      
    SELECT a.Productid,a.color+ ' missed' ProductColor   
    FROM pairs a  
    LEFT JOIN productinfo b  
           ON b.Productid=a.Productid and a.color=b.Productcolor  
    WHERE b.Productid IS NULL    
    

    Output:

     Productid    ProductColor  
     2    white missed  
     3    blue missed  
     3    white missed  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Visakh 211 Reputation points Volunteer Moderator
    2021-01-07T07:40:33.14+00:00

    @tani oju
    I would say safest method would be not to hardcode the colors as we cant be sure it will be always Red, Blue and White alone and not anything else
    Better use the table itself as reference to get distinct color list and use it for getting missed one for each ids so that regardless of the colors that are actually present, the logic will always work fine

    Like

    SELECT i.Productid, c.Productcolor  
    FROM (SELECT DISTINCT Productid FROM productinfo) i  
    CROSS JOIN (SELECT DISTINCT Productcolor FROM productinfo) c  
    WHERE NOT EXISTS  
    (  
    SELECT 1  
    FROM productinfo  
    WHERE Productid = i.Productid  
    AND Productcolor = c.Productcolor  
    )  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.