<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/rss.xsl" type="text/xsl"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"

>
<channel>
	<title>Sacha Chua - tag - sql</title>
	<atom:link href="https://sachachua.com/blog/tag/sql/feed/index.xml" rel="self" type="application/rss+xml" />
	<atom:link href="https://sachachua.com/blog/tag/sql" rel="alternate" type="text/html" />
	<link>https://sachachua.com/blog/tag/sql/feed/index.xml</link>
	<description>Emacs, sketches, and life</description>
  
	<lastBuildDate>Mon, 06 Jul 2026 16:20:27 GMT</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>daily</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>11ty</generator>
  <item>
		<title>Finding missing dates in PostgreSQL</title>
		<link>https://sachachua.com/blog/2015/06/finding-missing-dates-in-postgresql/</link>
		<dc:creator><![CDATA[Sacha Chua]]></dc:creator>
		<pubDate>Thu, 18 Jun 2015 23:42:00 GMT</pubDate>
    <category>geek</category>
		<guid isPermaLink="false">https://sachachua.com/blog/?p=28294</guid>
		<description><![CDATA[<p>My analytics numbers were way off from what I expected them to be. When I did a day-by-day comparison of my numbers and the reference set of numbers, I realized that a few weeks of data were missing from the year of data I was analyzing – a couple of days here, two weeks there, and so on. I manually identified the missing dates so that I could backfill the data. Since this was the second time I ran into that problem, though, I realized I needed a better way to catch this error and identify gaps.</p>
<p>Initially, I verified the number of days in my PostgreSQL database table with a SQL statement along the lines of:</p>
<div class="org-src-container">
<pre class="src src-sql"><span class="org-keyword">SELECT</span> <span class="org-keyword">year</span>, <span class="org-keyword">month</span>, <span class="org-builtin">COUNT</span>(*) <span class="org-keyword">AS</span> num_days <span class="org-keyword">FROM</span>
(<span class="org-keyword">SELECT</span> date_part(<span class="org-string">'year'</span>, day_ts) <span class="org-keyword">AS</span> <span class="org-keyword">year</span>,
 date_part(<span class="org-string">'month'</span>, day_ts) <span class="org-keyword">AS</span> <span class="org-keyword">month</span>,
 day_ts <span class="org-keyword">FROM</span> (<span class="org-keyword">SELECT</span> <span class="org-keyword">DISTINCT</span> day_ts <span class="org-keyword">FROM</span> table_with_data) <span class="org-keyword">AS</span> temp) <span class="org-keyword">AS</span> temp2
<span class="org-keyword">ORDER</span> <span class="org-keyword">BY</span> <span class="org-keyword">year</span>, <span class="org-keyword">month</span>
</pre>
</div>
<p>I checked each row to see if it matched the number of days in the month.</p>
<p>It turns out there&#8217;s an even better way to look for missing dates. PostgreSQL has a <code>generate_sequence</code> command, so you can do something like this:</p>
<div class="org-src-container">
<pre class="src src-sql"><span class="org-keyword">SELECT</span> missing_date
<span class="org-keyword">FROM</span> generate_series(<span class="org-string">'2015-01-01'</span>::<span class="org-type">date</span>, <span class="org-builtin">CURRENT_DATE</span> - <span class="org-type">INTERVAL</span> <span class="org-string">'1 day'</span>) missing_date
<span class="org-keyword">WHERE</span> missing_date <span class="org-keyword">NOT</span> <span class="org-keyword">IN</span> (<span class="org-keyword">SELECT</span> <span class="org-keyword">DISTINCT</span> day_ts <span class="org-keyword">FROM</span> table_with_data)
<span class="org-keyword">ORDER</span> <span class="org-keyword">BY</span> missing_date
</pre>
</div>
<p>Neat, huh?</p>

<p>You can <a href="https://sachachua.com/blog/2015/06/finding-missing-dates-in-postgresql/#comment">view 2 comments</a> or <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2015%2F06%2Ffinding-missing-dates-in-postgresql%2F&body=Name%20you%20want%20to%20be%20credited%20by%20(if%20any)%3A%20%0AMessage%3A%20%0ACan%20I%20share%20your%20comment%20so%20other%20people%20can%20learn%20from%20it%3F%20Yes%2FNo%0A">e-mail me at sacha@sachachua.com</a>.</p>]]></description>
		</item>
	</channel>
</rss>