Case Statement

Bone_12 361 Reputation points
2022-10-04T17:09:33.397+00:00

Hi,
I have the following code that doesn't quite work as I need it to, and wondered where I am going wrong

select      
distinct  
substring(apps.app_code,3,6) as app_code,  
apps.app_name,  
max(case when comp_data.comp_id = '1' and comp_data.comp_rec = '2' then substring(comp_data.comptext,3,7) else substring(apps.app_code,3,6) end)   
over (partition by apps.app_code) as parent_app_code  
  
from apps   
left join comp_data  
on apps.app_code = comp_data.comptext  

This is my output from the code used above:

247484-image.png

But this is the output that I am hoping to achieve:
247377-image.png

If I change the code to this below, I get the correct parent_app_code, but the rest of the records where they don't have a parent_app_code, it's just left with a blank value

 select      
    distinct  
    substring(apps.app_code,3,6) as app_code,  
    apps.app_name,  
    max(case when comp_data.comp_id = '1' and comp_data.comp_rec = '2' then substring(comp_data.comptext,3,7) else '' end)   
    over (partition by apps.app_code) as parent_app_code  
      
    from apps   
    left join comp_data  
    on apps.app_code = comp_data.comptext  

247502-image.png

I don't really understand why this doesn't work, can someone please point me in the right direction as to how I resolve. This is the sort of output that I need (app_code in green signals that it's the parent_app_code):

247492-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,697 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

Accepted answer
  1. CosmogHong-MSFT 22,861 Reputation points Microsoft Vendor
    2022-10-05T03:09:27.827+00:00

    Hi @Bone_12

    If I change the code to this below, I get the correct parent_app_code, but the rest of the records where they don't have a parent_app_code, it's just left with a blank value

    How about changing this blank value to NULL and then use ISNULL function to turn NULL value to 'app_code'.
    Like this:

    select distinct  
           substring(apps.app_code,3,6) as app_code,  
           apps.app_name,  
           ISNULL(max(case when comp_data.comp_id = '1' and comp_data.comp_rec = '2'then substring(comp_data.comptext,3,7) else NULL end) over(partition by apps.app_code)  
                 ,substring(apps.app_code,3,6)) as parent_app_code  
    from apps left join comp_data on apps.app_code = comp_data.comptext  
    

    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.


0 additional answers

Sort by: Most helpful