<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>sql on gnarlyware</title>
    <link>https://gnarlyware.com/tags/sql/</link>
    <description>Recent content in sql on gnarlyware</description>
    <generator>Hugo -- gohugo.io</generator>
    <managingEditor>fredrik.pj.nilsson@gmail.com (Fredrik Nilsson)</managingEditor>
    <webMaster>fredrik.pj.nilsson@gmail.com (Fredrik Nilsson)</webMaster>
    <lastBuildDate>Sat, 28 Aug 2021 00:00:00 +0000</lastBuildDate><atom:link href="https://gnarlyware.com/tags/sql/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>QUALIFY clause is now available in BigQuery</title>
      <link>https://gnarlyware.com/blog/qualify-clause-is-now-available-in-bigquery/</link>
      <pubDate>Sat, 28 Aug 2021 00:00:00 +0000</pubDate>
      <author>fredrik.pj.nilsson@gmail.com (Fredrik Nilsson)</author>
      <guid>https://gnarlyware.com/blog/qualify-clause-is-now-available-in-bigquery/</guid>
      <description>
&lt;script src=&#34;{{&lt; blogdown/postref &gt;}}index_files/header-attrs/header-attrs.js&#34;&gt;&lt;/script&gt;


&lt;p&gt;When browsing the &lt;a href=&#34;https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#qualify_clause&#34;&gt;BigQuery docs&lt;/a&gt;, I discovered that BigQuery now supports the &lt;code&gt;QUALIFY&lt;/code&gt;-clause. At time of writing, it’s in &lt;a href=&#34;https://cloud.google.com/products#product-launch-stages&#34;&gt;preview&lt;/a&gt;, but you can already try it out.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;QUALIFY&lt;/code&gt;-clause lets you filter on the output of &lt;a href=&#34;https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts&#34;&gt;analytic functions&lt;/a&gt;. This allows me to scratch an itch I’ve had for a while:&lt;/p&gt;
&lt;div id=&#34;for-each-distinct-value-in-column-x-find-the-row-with-max-value-in-column-y&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;For each distinct value in column X, find the row with max value in column Y&lt;/h2&gt;
&lt;p&gt;A common pattern is: “For each distinct value in column X, find the row where column Y takes it’s maximum value”. You can accomplish this by using &lt;code&gt;ROW_NUMBER() OVER (PARTITION BY X ORDER BY Y DESC) AS rank&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;An example of this would be to &lt;strong&gt;find the date at which each of a set of countries reported the largest number of new confirmed covid-19 cases&lt;/strong&gt;. We’ll be using the
&lt;code&gt;bigquery-public-data.covid19_open_data&lt;/code&gt; dataset.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
