<?xml version="1.0" encoding="UTF-8"?>
<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>web(cslai) &#187; SQL</title>
	<atom:link href="http://cslai.coolsilon.com/category/web-development/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://cslai.coolsilon.com</link>
	<description>Findings and Notes in Web Development</description>
	<lastBuildDate>Tue, 06 Sep 2011 08:15:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Magic SQL</title>
		<link>http://cslai.coolsilon.com/2008/09/03/magic-sql/</link>
		<comments>http://cslai.coolsilon.com/2008/09/03/magic-sql/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 13:21:00 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=50</guid>
		<description><![CDATA[Ever wanted to find the number of days between two dates without counting weekend (Saturdays and Sundays)? In PHP you typically needs to do a lot of calculation and a lot of factors needs to be considered. Therefore, in the end you will end up having a whole bunch of code that you will probably [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to find the number of days between two dates without counting weekend (Saturdays and Sundays)? In PHP you typically needs to do a lot of calculation and a lot of factors needs to be considered. Therefore, in the end you will end up having a whole bunch of code that you will probably start asking yourself whether you are programming a web-calendar or something similar.</p>
<p><span id="more-50"></span></p>
<p>I am currently working on a projects that deals with a lot of time-related data. My mind went completely blank when I am asked to do the above calculation without PHP code but a simple SQL statement. I was like &#8216;What??! That&#8217;s impossible&#8230;&#8217;. However, it is very much possible and at least there seems to be a few ways to do it in almost all DBMS on market.</p>
<p>No, there will be no stored procedure involved. You will know how later.</p>
<p>At first I was trying to write a super-duper-extremely complex SQL. But after flipping through my old Database Systems (Connolly &amp; Begg, 2005) textbook (yes, I brought my super heavy database textbook to the office and just left there as my reference, I cannot program without reference), I made the first conclusion &#8212; the question given was insane. But if my supervisor already has the solution, then there must be a way to do it&#8230;.</p>
<p>First, I search through a <a href="http://www.google.com">famous search engine</a> and found <a href="http://chrismay.org/2008/03/30/Excluding+Weekends+From+A+SQL+Date+Range.aspx">this</a>, but it is for SQL server.</p>
<p>Then, I tried to do some calculation with timestamp data with postgresql based on the <a href="http://www.postgresql.org/docs/8.3/interactive/functions-datetime.html">documentation</a>.<br />
However, there doesn&#8217;t really work because I cannot &#8216;port&#8217; the SQL statement for SQL server to PostgreSQL. So I need to get a better way of doing that.</p>
<p>Then I need to find a way for me to generate a list in a column so I can use a count() aggregate function. But does it even exists, none that I know of. So I made another search and got <a href="http://www.postgresql.org/docs/8.3/interactive/functions-srf.html">this</a>. By combining tutorials and understanding from the documentation on date calculation, I then got the first query that looks like follows:-</p>
<p><code class="sql"><br />
SELECT  count(s.a + date '2008-8-26') AS days<br />
FROM    generate_series(0, (date '2008-9-1' - date '2008-8-26')) as s(a)<br />
WHERE   EXTRACT(isodow FROM s.a + date '2008-8-26') &lt; 6;<br />
</code></p>
<p>Alright, this doesn&#8217;t look very understandable, by comparing the answer provided by my supervisor (he published right before I posted mine). I got this</p>
<p><code class="sql"><br />
SELECT  COUNT(*) AS total_days<br />
FROM    (SELECT date '2008-8-26' + generate_series(0, (date '2008-9-1' - date '2008-8-26')) as all_days) AS calendar<br />
WHERE   EXTRACT(isodow FROM all_days) &lt; 6;<br />
</code></p>
<p>This is a much cleaner and understandable answer (The <a href="http://pastebin.com/pastebin.php?diff=f489c9da6">diff</a> between two queries). However, this is not an optimal answer and takes 2ms to count the number of days without weekends in a year (<a href="http://twitter.com/angch/statuses/899052665">source</a>).</p>
<p>So, isn&#8217;t this solution better than a typical PHP code?</p>
<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2008/09/03/magic-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

