How to make this SQL line more efficient? (Case when in Partition By)

前端 未结 2 548
春和景丽
春和景丽 2021-01-26 03:45

I have a SQL line that looks like this

,SUM(Unit_Retail) OVER (PARTITION BY CASE WHEN @LocalDetailLevel = \'master\' THEN Master_Item WHEN @LocalDetailLevel = \'         


        
2条回答
  •  孤城傲影
    2021-01-26 04:14

    From an efficiency perspective,

    CASE
        WHEN @LocalDetailLevel = 'master' THEN SUM(Unit_Retail) OVER (PARTITION BY Master_Item)
        WHEN @LocalDetailLevel = 'size' THEN SUM(Unit_Retail) OVER (PARTITION BY Item_Number)
        WHEN @LocalDetailLevel = 'color' THEN CONCAT(Item_Number, Color_Code)
    END AS Sum_Unit_Retail
    

    Will you your best bet here. Without knowing the intricacies of your data, this seems to logically take the least amount of operations to complete, which is a good sign from an efficiency perspective.

    If you'd like, you could compare it against your current performance by following the steps here.

提交回复
热议问题