Categories: Snowflake

Cool Stuff in Snowflake – Part 10: RATIO_TO_REPORT

I’m doing a little series on some of the nice features/capabilities in Snowflake (the cloud data warehouse). In each part, I’ll highlight something that I think it’s interesting enough to share. It might be some SQL function that I’d really like to be in SQL Server, it might be something else.

This episode talks about a new window function Snowflake recently introduced: RATIO_TO_REPORT. The function returns the ratio of the value of the current row to the sum of the values within the set. Or in other words, some sort of “percentage of total”. Nothing we couldn’t calculate before, but a bit of syntactic sugar so we don’t have to write two expressions.

An example using the StackOverflow database:

WITH CTE_SourceData AS
(
SELECT
     MONTH(p.POSTCREATIONDATE)  AS MonthNbr
    ,pt.POSTTYPEDESC
    ,COUNT(p.POSTID)            AS Cnt 
FROM STACKOVERFLOW.DBO.POSTS    p
JOIN STACKOVERFLOW.DBO.POSTTYPE pt ON p.POSTTYPEID = pt.POSTTYPEID
WHERE YEAR(p.POSTCREATIONDATE) = 2018
GROUP BY pt.POSTTYPEDESC,MonthNbr
)
SELECT
     MonthNbr
    ,PostTypeDesc
    ,Cnt
    ,RATIO_TO_REPORT(Cnt) OVER (PARTITION BY MonthNbr) AS PctOfTotal
FROM CTE_SourceData
ORDER BY MonthNbr, PctOfTotal DESC;

The query calculates the percentage of total of the different post types for each month. This gives the following result:

If you’re wondering, here’s how we would originally write the SQL expression (in SQL Server for example):

If you’re in the area of Mechelen, Belgium at the 8th of October 2019, you can catch me talking about Snowflake at DataMinds Connect. If you’re interested, drop by and say hi!


------------------------------------------------
Do you like this blog post? You can thank me by buying me a beer 🙂
Koen Verbeeck

Koen Verbeeck is a Microsoft Business Intelligence consultant at AE, helping clients to get insight in their data. Koen has a comprehensive knowledge of the SQL Server BI stack, with a particular love for Integration Services. He's also a speaker at various conferences.

View Comments

  • I believe this has been in Oracle since 1999 or 2003. Percent to Total is useful over a window for sure.

Recent Posts

Free webinar – Tackling the Gaps and Islands Problem with T-SQL Window Functions

I'm hosting a free webinar at MSSQLTips.com at the 19th of December 2024, 6PM UTC.…

6 days ago

dataMinds Connect 2024 – Session Materials

The slides and scripts for my session "Tackling the Gaps & Islands Problem with T-SQL…

4 weeks ago

Connect to Power BI as a Guest User in another Tenant

Sometimes your Microsoft Entra ID account (formerly known as Azure Active Directory) is added as…

2 months ago

How to use a Script Activity in ADF as a Lookup

In Azure Data Factory (ADF, but also Synapse Pipelines and Fabric Pipelines), you have a…

4 months ago

Database Build Error – Incorrect syntax near DISTINCT

I wrote a piece of SQL that had some new T-SQL syntax in it: IS…

4 months ago

Speaking at dataMinds Connect 2024

I'm very excited to announce I've been selected as a speaker for dataMinds Connect 2024,…

5 months ago