R_Type_Name according to Range

Analyst_SQL 3,531 Reputation points
2021-10-20T13:13:11.093+00:00

I want R_Typ_Range according to range ,below is data

 Create table #tbl_Range (R_type_ID int,R_Type_Name varchar(50),R_Value_From decimal(10,4),R_Value_to decimal(10,4))  
 Create table #tbl_issuance  (issue_ID int,item_code int,Issue_Date int,Value decimal(10,4))  
  
 Insert into #tbl_Range values(1,'A',0.1000 ,0.5000)  
  Insert into #tbl_Range values(2,'B',0.6000 ,0.8000)  
   Insert into #tbl_Range values(3,'C',0.9000 ,0.10000)  
  
   insert into #tbl_issuance  values (1001,1,'2021-10-20',0.2200)  
   insert into #tbl_issuance  values (1002,2,'2021-10-20',0.6200)  
   insert into #tbl_issuance  values (1003,2,'2021-10-20',0.9200)    
  
  

if value in #tbl_issuance exit between 0.1000 to 0.5000 in #tbl_Range table then R_Type_Name will be 'A'
if value in #tbl_issuance exit between 0.6000 to 0.8000 in #tbl_Range table then R_Type_Name will be 'B'
if value in #tbl_issuance exit between 0.9000 to 0.10000 in #tbl_Range table then R_Type_Name will be 'C'

Output

142079-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
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
0 comments No comments
{count} votes

Accepted answer
  1. Nasreen Akter 10,736 Reputation points
    2021-10-20T14:01:36.73+00:00

    Hi @Analyst_SQL ,

    Would you please try the following. Thanks!

    Create table #tbl_Range (R_type_ID int,R_Type_Name varchar(50),R_Value_From decimal(10,4),R_Value_to decimal(10,4))  
    Create table #tbl_issuance  (issue_ID int,item_code int,Issue_Date date, Value decimal(10,4))  
          
    Insert into #tbl_Range values(1,'A',0.1000 ,0.5000)  
    Insert into #tbl_Range values(2,'B',0.6000 ,0.8000)  
    Insert into #tbl_Range values(3,'C',0.9000 ,1.0000)  
          
    insert into #tbl_issuance  values (1001,1,'2021-10-20',0.2200)  
    insert into #tbl_issuance  values (1002,2,'2021-10-20',0.6200)  
    insert into #tbl_issuance  values (1003,2,'2021-10-20',0.9200)    
      
    select i.issue_ID, i.item_code, FORMAT(i.Issue_Date, 'MM/dd/yyyy') as Issue_Date, i.Value, r.R_Type_Name from #tbl_issuance as i  
    left join #tbl_Range as r on (i.Value >= r.R_Value_From AND i.Value <= r.R_Value_to)  
    

    142018-image.png


0 additional answers

Sort by: Most helpful