Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2014
    Posts
    268
    Provided Answers: 6

    Answered: Using OVER Clause

    I've always user over with partitioning however recently I am looking at some other code and they have calculating percentages using over. I'm at a lost on why you would do this.

    Example would be

    CAST(TABLE1.UNIT AS DECIMAL (6,2)) / SUM(TABLE2.UNIT) OVER () AS UNIT_%

    What about be the advantage of using OVER there?

    Thanks!

  2. Best Answer
    Posted by Thrasymachus

    "Logically I can not think of a reason.

    You certainly get a much less complicated execution plan without the over clause. take a look at the difference between this...

    SELECT CAST(1 AS DECIMAL (6,2)) / SUM(1) OVER () AS UNIT

    and

    SELECT CAST(1 AS DECIMAL (6,2)) / SUM(1) AS UNIT

    with the show execution plan turned on. With the over clause there is a Nested Joins operator that incurs 75% of the query cost because it is expecting a partition. Without the over clause the scan and the stream aggregate consume all of the cost."


  3. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Logically I can not think of a reason.

    You certainly get a much less complicated execution plan without the over clause. take a look at the difference between this...

    SELECT CAST(1 AS DECIMAL (6,2)) / SUM(1) OVER () AS UNIT

    and

    SELECT CAST(1 AS DECIMAL (6,2)) / SUM(1) AS UNIT

    with the show execution plan turned on. With the over clause there is a Nested Joins operator that incurs 75% of the query cost because it is expecting a partition. Without the over clause the scan and the stream aggregate consume all of the cost.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  4. #3
    Join Date
    Oct 2014
    Posts
    268
    Provided Answers: 6
    Thanks for following up, I have never seen over used in such a manor.

    I'l review as suggested thank you very much.

  5. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    thank you. finally scored a best answer.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •