T-SQL Tutorial - Aggregate Window Functions Performance

Поделиться
HTML-код
  • Опубликовано: 6 сен 2024

Комментарии • 20

  • @OldPick-Unix-dude-pb9jg
    @OldPick-Unix-dude-pb9jg Год назад +2

    As usual, I find your video quite helpful and useful. Thanks again.

  • @JohnBoen
    @JohnBoen 2 года назад +3

    Been using SQL since 95, and didn't use the OVER() stuff specifically because of performance; I tend to favor derived tables.
    But I never tested again when 2019 shipped.
    Thanks for teaching this old dog :)

    • @BeardedDevData
      @BeardedDevData  2 года назад +1

      Fantastic, window functions performance has always been with lag and lead or at least it has for sometime but its great to see aggregation performance improved.

    • @JohnBoen
      @JohnBoen 2 года назад +1

      @@BeardedDevData
      You present things well.
      Maybe my real problem was familiarity. I wrote a lot of SQL before that stuff was introduced and it seems to be the way I think...
      I'm sold. Windowing sure does make things like moving average easier to calculate...
      Are you playing with the 2022 preview? Does it have window and nth_row?

    • @BeardedDevData
      @BeardedDevData  2 года назад +1

      I mainly work in Azure now, the new features are available in Azure SQL Database first which is quite cool.

    • @houstonvanhoy7767
      @houstonvanhoy7767 Год назад

      @@BeardedDevData Does that mean that a video about Azure SQL is in your plans? Perhaps new features, for example.

  • @sravankumar1767
    @sravankumar1767 2 года назад +1

    Nice explanation 👌 👍

  • @Sttuey
    @Sttuey 2 года назад +1

    rand() is executed once per batch rather than for each row.

    • @BeardedDevData
      @BeardedDevData  2 года назад

      Perfect explanation.

    • @houstonvanhoy7767
      @houstonvanhoy7767 Год назад

      Meaning, I think, that the same value generated by rand() would be inserted into the amount field for all rows created by the cross joins. Right?

    • @Sttuey
      @Sttuey Год назад

      @@houstonvanhoy7767 Well, you don't have to wonder, you can test it.

  • @ameobiamicrobiological2366
    @ameobiamicrobiological2366 2 года назад +1

    I do the best that I can to avoid functions where possible and I'm not familiar with this particular one, my only guess is that you would take a performance hit. But please let me know how far off I am? 😄👍

    • @BeardedDevData
      @BeardedDevData  2 года назад

      Why do you avoid functions? In this case Microsoft has extended functionality over the last few years so performance is actually much better when using Window Functions than the alternative but it depends what version of SQL Server you are using. I would never recommend a blanket approach of avoidance, that can certainly cause sub-optimal queries, e.g. table-valued functions are fantastic, keep an eye out for a video on those soon.

  • @CaribouDataScience
    @CaribouDataScience 2 года назад +1

    Ok, this is where I am at.
    SELECT
    [Customer Name],
    ROUND(SUM([Sales]),2) as customer_total
    FROM Orders O
    GROUP BY [Customer Name];
    CROSS JOIN
    (
    SELECT [Customer Name],
    SUM(Sales) OVER(PARTITION BY [Customer Name]) as customer_total,
    SUM(Sales) OVER() as total_sales,
    ROUND((SUM(Sales) OVER(PARTITION BY [Customer Name])/SUM(Sales) OVER() ) *100,3) as percent_of_total
    FROM Orders
    ORDER BY percent_of_total desc
    ) AS W
    ON O.[Customer Name] = W.[Customer Name];

    • @BeardedDevData
      @BeardedDevData  2 года назад

      Not quite, as you only want to know customers, total by customer, total and percentage of total you can really simplify the query:
      SELECT
      [Customer Name],
      [CustomerTotal],
      Total,
      ([CustomerTotal] / [Tota]l) * 100 AS [Percentage]
      FROM
      (
      SELECT
      [Customer Name],
      SUM(Sales) AS CustomerTotal
      FROM dbo.Orders
      GROUP BY [Customer Name]
      ) AS C
      CROSS JOIN
      (
      SELECT
      SUM(Sales) AS Total
      FROM dbo.Orders
      ) AS D

    • @CaribouDataScience
      @CaribouDataScience 2 года назад +1

      @@BeardedDevData Close but no cigar? I am getting this error:
      Msg 8120, Level 16, State 1, Line 2
      Column 'dbo.Orders.Customer Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    • @BeardedDevData
      @BeardedDevData  2 года назад

      No problem, was replying late last night and missed off the GROUP BY, edited the original query now

    • @CaribouDataScience
      @CaribouDataScience 2 года назад +1

      @@BeardedDevData Thanks again for helping me with this. when I run the above query there is a error message:
      Msg 8120, Level 16, State 1, Line 4
      Column 'D.Total' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    • @BeardedDevData
      @BeardedDevData  2 года назад +1

      That was me trying to point you in the right direction, I didn't want to just write the solution for you but have you think about it. I have updated it again to the final query although I haven't fully checked the syntax.