<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dhairya Pandya</title>
    <description>The latest articles on DEV Community by Dhairya Pandya (@dhairya_pandya).</description>
    <link>https://dev.to/dhairya_pandya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3912423%2F7208d0c2-eda9-4f10-893c-4bb9ba669d40.png</url>
      <title>DEV Community: Dhairya Pandya</title>
      <link>https://dev.to/dhairya_pandya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhairya_pandya"/>
    <language>en</language>
    <item>
      <title>How I was introduced to SQL window function</title>
      <dc:creator>Dhairya Pandya</dc:creator>
      <pubDate>Mon, 04 May 2026 17:32:29 +0000</pubDate>
      <link>https://dev.to/dhairya_pandya/how-i-was-introduced-to-sql-window-function-292m</link>
      <guid>https://dev.to/dhairya_pandya/how-i-was-introduced-to-sql-window-function-292m</guid>
      <description>&lt;p&gt;I was called at my firm for having to create a analytics dashboard for a &lt;em&gt;multiTenet&lt;/em&gt; CRM dashboard where the magnitude of rows were in thousands and the efficiency i could not blame postgress or servers I had to figure out how to &lt;em&gt;index, query and cache&lt;/em&gt; the data. &lt;/p&gt;

&lt;p&gt;The business questions were something like this&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Give the sales agents aggregated sales data from the month of January, till the current month in waterfall structure so that the numbers are aggregated in that order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ranking the sales agents on the basis of their quaterly performance to reward the winner on the basis of how much sales they made and the repeating customers that were coming in from that agent. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The issue was simple I had no idea that there is something called as a &lt;em&gt;window function&lt;/em&gt;. So I took my beginers head and started to query the data. Where I failed to make a query for the correct required data, partly because I was still a noob, if I had known sql well i would have solved the problem. &lt;/p&gt;

&lt;p&gt;| How do I aggregate the rows of data without collapsing&lt;/p&gt;

&lt;h3&gt;
  
  
  A simple structure of window function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;FUNCTION_NAME&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt;
  &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt;
  &lt;span class="k"&gt;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference between a GROUP BY and WINDOW FUNCTION is that a group by will collapse&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;rep&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RANK&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt;
  &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;rank_in_region&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales_reps&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Partition By&lt;/strong&gt; will make sure that the rows are split in groups. Each region is processed independently like running query separately for each group.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="k"&gt;month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;revenue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;revenue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;month&lt;/span&gt;
    &lt;span class="k"&gt;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;running_total&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;monthly_revenue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;UNBOUND PRECEDING&lt;/strong&gt; Frame starts at row 1 always. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PARTITION BY&lt;/strong&gt; The entire table is one window. Compare to Query 1 where PARTITION BY created separate window per region. &lt;/p&gt;

</description>
      <category>analytics</category>
      <category>database</category>
      <category>postgres</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
