<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Brian Mwathi on Medium]]></title>
        <description><![CDATA[Stories by Brian Mwathi on Medium]]></description>
        <link>https://medium.com/@mwathibrian7?source=rss-3d6c57f2b96d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*--p8qFWfgjs0HI2mLH28pA.jpeg</url>
            <title>Stories by Brian Mwathi on Medium</title>
            <link>https://medium.com/@mwathibrian7?source=rss-3d6c57f2b96d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 15:03:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mwathibrian7/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Handy PostgreSQL Queries for Reporting (MTD, QTD, YTD)]]></title>
            <link>https://medium.com/@mwathibrian7/handy-postgresql-queries-for-reporting-mtd-qtd-ytd-38c525317c2c?source=rss-3d6c57f2b96d------2</link>
            <guid isPermaLink="false">https://medium.com/p/38c525317c2c</guid>
            <category><![CDATA[reporting]]></category>
            <category><![CDATA[postgresql]]></category>
            <dc:creator><![CDATA[Brian Mwathi]]></dc:creator>
            <pubDate>Fri, 28 Feb 2020 11:39:10 GMT</pubDate>
            <atom:updated>2020-02-29T03:10:41.974Z</atom:updated>
            <content:encoded><![CDATA[<h3>Handy PostgreSQL Queries for Reporting (WTD, MTD, QTD, YTD)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Nt46iW30UWy8Fruo" /><figcaption>Photo by <a href="https://unsplash.com/@bepnamanh?utm_source=medium&amp;utm_medium=referral">Nam Anh</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>At some point you have been tasked to get the “<em>to-date</em>” values of records (<em>e.g sales, orders, users registered</em>) in database. This may be month-to-date (MTD), year to date (YTD), quarter to date (QTD). This tutorial will show you how to lookup for such records in PostgreSQL, using the inbuilt <strong>current_date</strong> and <strong>extract</strong> date functions.</p><p>Consider the following users table:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee5bf7f0648780fbfd239484e43e8615/href">https://medium.com/media/ee5bf7f0648780fbfd239484e43e8615/href</a></iframe><p>To get users created <em>(</em><strong><em>week/month/quarter/year)</em>-to-date</strong> we need to confirm that the following conditions are met in the record’s corresponding <strong><em>created_at </em></strong>field:</p><ul><li>The (week/month/quarter/year) is the same as the current (week/month/quarter/year)</li><li>The year is the same to the current year ( For YTD, this is the only condition you check)</li></ul><p>Consider the query to get the users registered month to date:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5947da9085f269f80e0f909db80dbf3/href">https://medium.com/media/e5947da9085f269f80e0f909db80dbf3/href</a></iframe><p>Below is an illustration of the comparison check each row undergoes when the above query is run</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZjrtOsY1jihea-fH5dCIOQ.png" /></figure><p>The<strong><em> current_date </em></strong><em>—</em><strong><em> </em></strong>You guessed it! This returns the current date.</p><p>The <a href="https://www.postgresql.org/docs/9.1/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT"><strong><em>extract</em></strong><em>( subfield </em><strong><em>from</em></strong><em> source)</em></a><em> </em>function<em> — </em>Gets the corresponding <em>subfield</em> such as (week/month/quarter/year) — <em>year</em> in our case — from a <em>source</em> of date/time type — <em>created_at</em> column in our case.</p><p>Since you are now familiar with the extract function, you can now easily get the (week / quarter) to date values by replacing the keyword month with either week or quarter. This would look as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da1ca6c3070d2959ba98d6bd8a972725/href">https://medium.com/media/da1ca6c3070d2959ba98d6bd8a972725/href</a></iframe><p><em>Note: Make sure to add the check:<br></em><strong>extract</strong>(<strong>year</strong> <strong>from</strong> &lt;column&gt;) = <strong>extract</strong>(<strong>year</strong> <strong>from</strong> current_date)<em> <br>this will make sure you get the records filled in the current year. Otherwise you will get misleading results i.e records that span across various years.</em></p><p>If you do have a better or optimal way to go about this, you can post it in the comment. Your feedback is highly encouraged.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38c525317c2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>