Case Statement Optimization

  • CASE WHEN

    (

    Table1.Column IN (@Parameter1)

    OR b.cat3 = 1

    OR (Table1.Column='XXXX' AND SV.Column IN ('XXX'))

    OR (Table1.Column='XXXX' AND SV.Column IN ('XXX') )

    And C.value = 1

    )

    Case when (

    Table1.Column IN (@Parameter1)

    OR b.cat3 = 1

    OR (Table1.Column='XXXX' AND SV.Column IN ('XXX'))

    OR (Table1.Column='XXXX' AND SV.Column IN ('XXX') )

    And C.value = 2

    )

    I am seeing Duplicate code in Case statements in select Statement how to optimize this code.

     

  • The fact that you're getting duplicates is an issue with your logic.  You haven't given us enough information to troubleshoot the logic, but it's probably do to the fact that your last condition is irrelevant.

    This code

    Table1.Column IN (@Parameter1)
    OR b.cat3 = 1
    OR (Table1.Column='XXXX' AND SV.Column IN ('XXX'))
    OR (Table1.Column='XXXX' AND SV.Column IN ('XXX') )
    And C.value = 1

    is logically equivalent to this code.

    Table1.Column IN (@Parameter1)
    OR b.cat3 = 1
    OR (Table1.Column='XXXX' AND SV.Column IN ('XXX'))

    Similarly with the second case expression.  So the fact that you're getting the same value for both expressions is because both expressions are logically equivalent.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply