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 :)
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.
@@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?
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? 😄👍
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.
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];
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
@@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 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.
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.