<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/atom.xsl" type="text/xsl"?><feed
	xmlns="http://www.w3.org/2005/Atom"
	xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="en-US"
	><title>Sacha Chua - tag - postgresql</title>
	<subtitle>Emacs, sketches, and life</subtitle>
	<link rel="self" type="application/atom+xml" href="https://sachachua.com/blog/tag/postgresql/feed/atom/index.xml" />
  <link rel="alternate" type="text/html" href="https://sachachua.com/blog/tag/postgresql" />
  <id>https://sachachua.com/blog/tag/postgresql/feed/atom/index.xml</id>
  <generator uri="https://11ty.dev">11ty</generator>
	<updated>2015-06-18T23:42:00Z</updated>
<entry>
		<title type="html">Finding missing dates in PostgreSQL</title>
		<link rel="alternate" type="text/html" href="https://sachachua.com/blog/2015/06/finding-missing-dates-in-postgresql/"/>
		<author><name><![CDATA[Sacha Chua]]></name></author>
		<updated>2015-06-20T00:15:40Z</updated>
    <published>2015-06-18T23:42:00Z</published>
    <category term="geek" />
		<id>https://sachachua.com/blog/?p=28294</id>
		<content type="html"><![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>]]></content>
		</entry>
</feed>