case condition in min max values

Shambhu Rai 1,406 Reputation points
2022-07-14T19:15:04.293+00:00

Hi Expert,

I wanted to update the target table based on the conversation table . if the value is between 100 and 200 then

if the value is between 200 and 400 then

create table source1
(car int,cycle int)
insert into source1
values(150,250)
220826-image.png

create table conversation_table
(min int ,max int, speed char, rating char)
insert into conversation_table
values(100,200,'Higher','good'),
(200,400,'Highest','better')
220902-image.png

create table target1
(property char, value int, speed char, rating char)
expected output
220795-image.png

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,714 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,799 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,453 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

6 answers

Sort by: Most helpful
  1. Jingyang Li 5,891 Reputation points
    2022-07-14T19:44:43.93+00:00
    create table source1  
    (car int,cycle int)  
    insert into source1  
    values(150,250)  
       
      
    create table conversation_table  
    (min int ,max int, speed  varchar(30), rating varchar(30))  
    insert into conversation_table  
    values(100,200,'Higher','good'),  
    (200,400,'Highest','better')  
       
    ;with mycte   (TheKey, TheValue) as (  
    SELECT [KEY], Value from OpenJson((  
    select * from source1  FOR JSON AUTO ))  
    )   
        
     SELECT unpvt.[Key] as Property, ct.max as Value, ct.speed,ct.rating  
     FROM mycte AS src  
     CROSS APPLY OpenJson(src.TheValue) AS unpvt    
    Join conversation_table ct on unpvt.Value between ct.min and ct.max  
      
      
    drop table source1,conversation_table  
    

  2. Shambhu Rai 1,406 Reputation points
    2022-07-14T20:36:40.19+00:00

    Any other suggestion with simply case condition or easiest one


  3. Jingyang Li 5,891 Reputation points
    2022-07-14T20:46:18.567+00:00
     create table source1  
     (car int,cycle int)  
     insert into source1  
     values(150,250)  
           
          
     create table conversation_table  
     (min int ,max int, speed  varchar(30), rating varchar(30))  
     insert into conversation_table  
     values(100,200,'Higher','good'),  
     (200,400,'Highest','better')  
        
    Select Property, ct.max as Value, ct.speed,ct.rating   
    from source1  
    Unpivot  
    (  val for Property in (Car,cycle) ) as unpvt  
     Join conversation_table ct on unpvt.val between ct.min and ct.max  
          
          
     drop table source1,conversation_table  
    

  4. Isabellaz-1451 3,616 Reputation points
    2022-07-15T02:07:46.163+00:00

    Hi @Shambhu Rai

    How about change the source1 table like below

      create table source1  
      (Property varchar(20),value int)  
      insert into source1  
      values('cycle',150),('car',250)  
               
              
      create table conversation_table  
      (min int ,max int, speed  varchar(30), rating varchar(30))  
      insert into conversation_table  
      values(100,200,'Higher','good'),  
      (200,400,'Highest','better')  
      
      select * from source1  cross apply( select top 1* from conversation_table  where source1.value >=conversation_table.min and source1.value < conversation_table.max) p  
    

    result is like this:
    220934-image.png


  5. LiHong-MSFT 10,046 Reputation points
    2022-07-15T02:39:33.95+00:00

    Hi @Shambhu Rai
    Please check this query:

    ;WITH CTE AS  
    (  
     SELECT Property,[Value]  
     FROM source1   
     UNPIVOT([Value] FOR Property IN ([car],[cycle]))U  
    )  
    SELECT A.Property,B.max AS [Value],B.speed,B.rating  
    FROM CTE A JOIN conversation_table B ON A.Value BETWEEN B.min AND B.max  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.