<?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>Krishna Sunuwar &#187; MySQL</title>
	<atom:link href="http://www.krishnasunuwar.com.np/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.krishnasunuwar.com.np</link>
	<description></description>
	<lastBuildDate>Fri, 03 Feb 2012 04:39:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>CrossTab query (Pivot Table or Transformation of rows into columns)</title>
		<link>http://www.krishnasunuwar.com.np/2011/02/crosstab-query-pivot-table-or-transformation-of-rows-into-columns/</link>
		<comments>http://www.krishnasunuwar.com.np/2011/02/crosstab-query-pivot-table-or-transformation-of-rows-into-columns/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 06:51:11 +0000</pubDate>
		<dc:creator>Krish</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[CrossTab Query]]></category>
		<category><![CDATA[Pivot Table]]></category>

		<guid isPermaLink="false">http://www.krishnasunuwar.com.np/?p=531</guid>
		<description><![CDATA[CrossTab query is amazing, which helps generating reports and play with aggregate values. Excel/Ms Access gives nice user interface for Pivot Table. It&#8217;s way to transfer rows into column. It is more often used to generate matrix form of report. Some real-life example: 1. Give me last 3 months sales report product wise. (It may look [...]]]></description>
			<content:encoded><![CDATA[<p>CrossTab query is amazing, which helps generating reports and play with aggregate values. Excel/Ms Access gives nice user interface for Pivot Table. It&#8217;s way to transfer rows into column. It is more often used to generate matrix form of report. Some real-life example:</p>
<p>1. Give me last 3 months sales report product wise. (It may look like below)</p>
<pre>Month --&gt;  | Jan    |  Feb     | March
---------- | ------ | -------- | -------
Product A  | 3387   | 87985    | 2338
Product B  | 6386   | 67983    | 6374
Product C  | 3880   | 76988    | 9378
..... and product lists goes down ....</pre>
<p>2. Give me last 3 months sales report country wise. (It may look like below)</p>
<pre>Country -&gt; | USA    |  France  | Japan
---------- | ------ | -------- | -------
Jan        | 3387   | 87985    | 2338
Feb        | 6386   | 67983    | 6374
Mar        | 3880   | 76988    | 9378</pre>
<p>#1 and #2 can be ask like &#8216;Give me top-ten selling products in last 3 months, or give me top-ten selling countries in last 3 months&#8221;</p>
<p>If you see field name (column title)  in #1, it&#8217;s name of month (date), which is row data and similarly country is row data in #2. They are not field name in database table. It is possible to get reports like above by transforming rows into columns. This is what called Pivot Table, and can be done using CrossTab query. Let&#8217;s take bit example (example belows are used with MySQL):</p>
<p>1. Create Database, tables and insert dummy data. Use script below:</p>
<pre class="mysql" style="padding-left: 30px;">CREATE TABLE  `products` (
	`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
	`product` VARCHAR( 100 ) NOT NULL
);

CREATE TABLE  `sales` (
	`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
	`product_id` INT NOT NULL ,
	`qty` INT NOT NULL,
	`sales_date` DATETIME NOT NULL
)

INSERT INTO `products` (`id`, `product`) VALUES
(1, 'Fanta'),
(2, 'Coke'),
(3, 'Mirinda'),
(4, 'Sprite');

INSERT INTO `sales`
(`id`, `sold_date`, `product_id`, `qty`)
VALUES
(1, '2011-02-07 11:49:53', 1, 4),
(2, '2011-02-07 11:49:53', 1, 4),
(3, '2011-02-08 11:50:05', 1, 8),
(4, '2011-02-07 11:49:53', 2, 5),
(5, '2011-02-07 11:49:53', 3, 2),
(6, '2011-02-08 11:50:05', 2, 7),
(7, '2011-02-07 11:49:53', 3, 5),
(8, '2011-02-07 11:49:53', 4, 2),
(9, '2011-02-08 11:50:05', 3, 7),
(10, '2011-02-07 11:49:53', 2, 5),
(11, '2011-02-07 11:49:53', 3, 2),
(12, '2011-02-08 11:50:05', 4, 7);</pre>
<p>2. Determine output you want (in our case, below is format):</p>
<pre style="padding-left: 30px;">Date       | Fanta  | Coke   | Mirinda | Sprite
xxxx:xx:xx | x      | x      | x       | x
xxxx:xx:xx | x      | x      | x       | x</pre>
<p>3. Write query, run  it:</p>
<pre class="mysql" style="padding-left: 30px;">SELECT
	s.sales_date `Sales Date`,
	SUM(IF(p.product='Fanta', s.qty, 0)) Fanta,
	SUM(IF(p.product='Coke', s.qty, 0))  Coke,
	SUM(IF(p.product='Mirinda', s.qty, 0)) Mirinda,
	SUM(IF(p.product='Sprite', s.qty, 0))  Sprite
FROM
	products p,
	sales s
WHERE
	p.id = s.product_id
GROUP BY
	s.sales_date</pre>
<p>4. Output is as given below:</p>
<pre style="padding-left: 30px;">Date                 | Fanta  | Coke   | Mirinda | Sprite
2011-02-07 11:49:53  | 8      | 10     | 9       | 2
2011-02-08 11:50:05  | 8      | 7      | 7       | 7</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.krishnasunuwar.com.np/2011/02/crosstab-query-pivot-table-or-transformation-of-rows-into-columns/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Perl beginner &#8211; install and run hello world, MySQL test</title>
		<link>http://www.krishnasunuwar.com.np/2010/02/perl-beginner-install-and-run-hello-world/</link>
		<comments>http://www.krishnasunuwar.com.np/2010/02/perl-beginner-install-and-run-hello-world/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 13:10:35 +0000</pubDate>
		<dc:creator>Krish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.krishnasunuwar.com.np/?p=266</guid>
		<description><![CDATA[I am seasoned PHP programmer, been around ten years I am coding on PHP. Although I learn and tried many different programming languages, I always scare to give a try to Perl. I was in impression that Perl is difficult language so never get confident to try it, or never can arrange time for it. [...]]]></description>
			<content:encoded><![CDATA[<p>I am seasoned PHP programmer, been around ten years I am coding on PHP. Although I learn and tried many different programming languages, I always scare to give a try to Perl. I was in impression that Perl is difficult language so never get confident to try it, or never can arrange time for it. But, today as I was working on Asterisk for VoIP system, there&#8217;s was no choice left, but Perl.</p>
<p>I gave first try today in Ubuntu. It wasn&#8217;t that bad as I heard. Actually I was wrong about Perl, it was proved.</p>
<p>Below is how, I tried first hello world and first database application
<p><strong>Install Perl</strong></p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ <span class="kw2">sudo</span> apt-get <span class="kw2">install</span> <span class="kw2">perl</span></div>
</li>
</ol>
</div>
<p>&nbsp;</p>
<p>Don&#8217;t worry, if Perl is already installed, it do nothing. If you have Ubuntu server edition, Perl is already there.</p>
<p>&nbsp;</p>
<p>Create directory called perl_test. Create perl_test.pl file and save following content.</p>
<div class="geshi no perl">
<ol>
<li class="li1">
<div class="de1"><span class="co1">#!/usr/bin/perl</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#simple perl program to print the user input</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">print</span> <span class="br0">&#40;</span><span class="st0">&quot;Hello world! test goes this<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$inputline</span>=<span class="re4">&lt;stdin&gt;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">print</span> <span class="br0">&#40;</span><span class="re0">$inputline</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>&nbsp;</p>
<p>
Save file named as perl_test.pl and run following command.</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ <span class="kw2">sudo</span> <span class="kw2">chmod</span> +x perl_test.pl</div>
</li>
<li class="li1">
<div class="de1">$ .<span class="sy0">/</span>perl_test.pl</div>
</li>
</ol>
</div>
<p> &nbsp;</p>
<p>
<strong>Database Test:</strong><br />
Create perl_db.pl file and save following content.</p>
<div class="geshi no perl">
<ol>
<li class="li1">
<div class="de1"><span class="co1">#!/usr/bin/perl</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">use</span> strict;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">use</span> warnings;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">use</span> DBI;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">my</span> <span class="re0">$username</span>=<span class="st0">&#39;test&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">my</span> <span class="re0">$pass</span>=<span class="st0">&#39;test&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">my</span> <span class="re0">$db</span>=<span class="st0">&#39;test&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">my</span> <span class="re0">$dbh</span> = DBI-<span class="sy0">&gt;</span><span class="me1">connect</span><span class="br0">&#40;</span> <span class="st0">&quot;dbi:mysql:$db&quot;</span>, <span class="re0">$username</span>, <span class="re0">$pass</span>, <span class="br0">&#123;</span> <span class="st0">&#39;PrintError&#39;</span> =<span class="sy0">&gt;</span> <span class="nu0">1</span>, <span class="st0">&#39;RaiseError&#39;</span> =<span class="sy0">&gt;</span> <span class="nu0">1</span> <span class="br0">&#125;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">my</span> <span class="re0">$sql</span>=<span class="st0">&#39;select * from test&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">my</span> <span class="re0">$sql_handle</span>=<span class="re0">$dbh</span>-<span class="sy0">&gt;</span><span class="me1">prepare</span><span class="br0">&#40;</span><span class="re0">$sql</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$sql_handle</span>-<span class="sy0">&gt;</span><span class="me1">execute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">my</span> <span class="re0">@data</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">while</span> <span class="br0">&#40;</span><span class="re0">@data</span>=<span class="re0">$sql_handle</span>-<span class="sy0">&gt;</span><span class="me1">fetchrow_array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">print</span> <span class="kw3">join</span><span class="br0">&#40;</span><span class="st0">&quot;<span class="es0">\n</span>&quot;</span>,<span class="re0">@data</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p> &nbsp; </p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ <span class="kw2">sudo</span> <span class="kw2">chmod</span> +x perl_db.pl</div>
</li>
<li class="li1">
<div class="de1">$ <span class="kw2">sudo</span> .<span class="sy0">/</span>perl_db.pl</div>
</li>
</ol>
</div>
<p> &nbsp; </p>
<p>Make sure user test with password test and database test exits. And it has test table.</p>
<p> &nbsp; <br />
Haurry!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.krishnasunuwar.com.np/2010/02/perl-beginner-install-and-run-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

