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

前端 未结 2 553
春和景丽
春和景丽 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:17

    For us to help, we need a few things from you. Start with this blog on Getting Help With a Slow Query. A big thing we need is the execution plan via PasteThePlan.com

    One issue you could be running into is Parameter Sniffing. This is where the execution plan really helps see where your query is hitting performance walls.

    Regarding the last part of your question, where you created a new question...

    CASE is an expression and thus isn't used to control logical flow.

    CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING

    For your problem, you would use IF...ELSE.

    The syntax is very similar...

    IF @LocalDetailLevel = 'master' 
    BEGIN
    ...
    END
    
    IF @LocalDetailLevel = 'size'
    BEGIN
    ...
    END
    

提交回复
热议问题