Value lookup in multiple columns in a table in given row

satish nuthpally 1 Reputation point
2022-08-18T22:45:53.917+00:00

I'm looking to achieve my requirement. to define a ClientService1 based on the other columns lookup in table. In below table if Service1, Service2, Service3, Service4 either one column has value ABC and if all remaining columns null or it should not contain other values, then I'll call that row eligible for ClientService1

232661-image.png
Above scenario is eligible for ClientService1.

232615-image.png
Above scenario is not eligible for ClientService1. Bcz service1 row has CGF

ClientService2 In below table if Service1, Service2, Service3, Service4 either one column has values ABC, DEF and if all remaining columns null or it should not contain other values, then I'll call that row eligible for ClientService2

232626-image.png
Above scenario is eligible for ClientService2.

232671-image.png
Above scenario is not eligible for ClientService2. Bcz service3 column has XYZ

How can I write SQL query to achieve ClientService1 & ClientService2 requirements

Azure SQL Database
Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Isabellaz-1451 3,616 Reputation points
    2022-08-19T06:41:02.407+00:00

    Hi @satish nuthpally

    Maybe you can concat all the field ,then do a filter

    232744-image.png
    Best Regards,
    Isabella

    1 person found this answer helpful.
    0 comments No comments

  2. Viorel 122.6K Reputation points
    2022-08-19T05:12:18.837+00:00

    Check a preliminary approach:

    -- Eligible rows for ClientService1  
    select *  
    from MyTable  
    where  
        (select count(c) from   
            (select Service1 union select Service2 union select Service3 union select Service4 except select 'ABC') t(c)) = 0  
        and  
        (select count(c) from   
            (select Service1 union select Service2 union select Service3 union select Service4 intersect select 'ABC') t(c)) = 1  
      
    -- Eligible rows for ClientService2  
    select *  
    from MyTable  
    where  
        (select count(c) from   
            (select Service1 union select Service2 union select Service3 union select Service4 except (select 'ABC' union select 'DEF')) t(c)) = 0  
        and  
        (select count(c) from   
            (select Service1 union select Service2 union select Service3 union select Service4 intersect (select 'ABC' union select 'DEF')) t(c)) = 2  
    
    0 comments No comments

  3. Anurag Sharma 17,631 Reputation points
    2022-08-19T09:02:51.91+00:00

    You can try this once:

    create table testservice(service1 varchar(100),service2 varchar(100),service3 varchar(100),service4 varchar(100))  
      
    insert into testservice (service2) values ('ABC');  
    insert into testservice (service2,service1) values ('ABC','CGF');  
    insert into testservice (service2,service1) values ('ABC','DEF');  
    insert into testservice (service2,service1,service3) values ('ABC','DEF','XYZ');  
      
    SELECT   
    CASE WHEN 'ABC' in (service1,service2,service3,service4)   
    AND (ISNUMERIC(ISNULL(Service1,1)) + ISNUMERIC(ISNULL(Service2,1))   
    + ISNUMERIC(ISNULL(Service3,1)) + ISNUMERIC(ISNULL(service4,1)) = 3) THEN 'ClientService1'   
    WHEN 'ABC' in (service1,service2,service3,service4) and 'DEF' in (service1,service2,service3,service4)   
    AND (ISNUMERIC(ISNULL(Service1,1)) + ISNUMERIC(ISNULL(Service2,1))   
    + ISNUMERIC(ISNULL(Service3,1)) + ISNUMERIC(ISNULL(service4,1)) = 2)  
    THEN 'ClientService2' END  
    FROM testservice;  
    
    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.