This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Need some help to understand the SWQL logic...

Over in this thread, kindly helped me out with some code, and the broader SWQL bits I can follow. For example the SELECT, FROM, WHERE

What I'm not understanding is how the rest of the code works logic wise, and wondered if someone here can advise as racowi hasn't been online for a while it appears - and, to be fair, my reply is in the middle of a mess of other posts.

I get the first 3 lines, which is just filtering for specific strings in those records, but the rest is just over my head Grin - so does anyone want to take a punt at typing an explanation for me please?

WHEN EntPhysicalIndex like '1%' AND EntityName = 'Chassis' THEN '1' 
     WHEN EntPhysicalIndex like '2%' AND EntityName = 'Chassis' THEN '2'
     WHEN EntPhysicalIndex like '3%' AND EntityName = 'Chassis' THEN '3'
     WHEN EntPhysicalIndex like '4%' AND EntityName = 'Chassis' THEN '4'
     WHEN EntPhysicalIndex like '5%' AND EntityName = 'Chassis' THEN '5'
     WHEN EntPhysicalIndex like '6%' AND EntityName = 'Chassis' THEN '6'
     WHEN EntPhysicalIndex like '7%' AND EntityName = 'Chassis' THEN '7'
     WHEN EntPhysicalIndex like '8%' AND EntityName = 'Chassis' THEN '8'
     WHEN ContainedIn like '%1' AND EntityName like 'Port%' THEN '1' 
     WHEN ContainedIn like '%2' AND EntityName like 'Port%' THEN '2'
     WHEN ContainedIn like '%3' AND EntityName like 'Port%' THEN '3'
     WHEN ContainedIn like '%4' AND EntityName like 'Port%' THEN '4'
     WHEN ContainedIn like '%5' AND EntityName like 'Port%' THEN '5'
     WHEN ContainedIn like '%6' AND EntityName like 'Port%' THEN '6'
     WHEN ContainedIn like '%7' AND EntityName like 'Port%' THEN '7'
     WHEN ContainedIn like '%8' AND EntityName like 'Port%' THEN '8'
     WHEN ContainedIn like '1%' AND EntityName not like 'Port%' AND EntityName <> 'Chassis' THEN '1' 
     WHEN ContainedIn like '2%' AND EntityName not like 'Port%' AND EntityName <> 'Chassis' THEN '2'
     WHEN ContainedIn like '3%' AND EntityName not like 'Port%' AND EntityName <> 'Chassis' THEN '3'
     WHEN ContainedIn like '4%' AND EntityName not like 'Port%' AND EntityName <> 'Chassis' THEN '4'
     WHEN ContainedIn like '5%' AND EntityName not like 'Port%' AND EntityName <> 'Chassis' THEN '5'
     WHEN ContainedIn like '6%' AND EntityName not like 'Port%' AND EntityName <> 'Chassis' THEN '6'
     WHEN ContainedIn like '7%' AND EntityName not like 'Port%' AND EntityName <> 'Chassis' THEN '7'
     WHEN ContainedIn like '8%' AND EntityName not like 'Port%' AND EntityName <> 'Chassis' THEN '8'   
     ELSE '1'
END AS Member

Parents
  • CASE WHEN statements are basically 'if/then/else' statements that are processed in order.

    If EntPhysicalIndex starts with '1' (LIKE '1%') and the EntityName is exactly 'Chassis'  then return 1 (as a string because of the quotes).

    If that match happens, then stop checking the rest of them and present the string '1' as the field named Member (END AS Member).

    If it doesn't match, check the next one for a match.  If none of them work, then (missing here) assign whatever is in the ELSE clause to the field Member.

    SELECT Field1 AS [First Field]
         , CASE
             WHEN ( Locigal Check 1 ) THEN ( Results if match )
             WHEN ( Locigal Check 2 ) THEN ( Other results if match )
             ELSE ( Results if no match )
           END AS [Second Field]
         , Field3 AS [Third Field]
    FROM [Table/Entity]

    The CASE statement work the exact same way (to my knowledge) as the T-SQL CASE statement, so you can always refer to the MS Docs if needed.

Reply
  • CASE WHEN statements are basically 'if/then/else' statements that are processed in order.

    If EntPhysicalIndex starts with '1' (LIKE '1%') and the EntityName is exactly 'Chassis'  then return 1 (as a string because of the quotes).

    If that match happens, then stop checking the rest of them and present the string '1' as the field named Member (END AS Member).

    If it doesn't match, check the next one for a match.  If none of them work, then (missing here) assign whatever is in the ELSE clause to the field Member.

    SELECT Field1 AS [First Field]
         , CASE
             WHEN ( Locigal Check 1 ) THEN ( Results if match )
             WHEN ( Locigal Check 2 ) THEN ( Other results if match )
             ELSE ( Results if no match )
           END AS [Second Field]
         , Field3 AS [Third Field]
    FROM [Table/Entity]

    The CASE statement work the exact same way (to my knowledge) as the T-SQL CASE statement, so you can always refer to the MS Docs if needed.

Children