<?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>PIVOT Table &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/pivot-table/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Tue, 13 Sep 2016 19:42:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: CREATE PIVOT TABLE to arrange Rows into Columns form</title>
		<link>https://www.dbrnd.com/2016/09/postgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause/</link>
					<comments>https://www.dbrnd.com/2016/09/postgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 13 Sep 2016 19:41:34 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[PIVOT Table]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3618</guid>

					<description><![CDATA[In this post, I am going to demonstrate arrangement of the row data to columns which is called a something like Pivot table in PostgreSQL. What is a Pivot Table? Pivot table is one kind of summary and representation of data, like Microsoft Spreadsheets. Pivot table arranges some of row categories into column and also [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause%2F&amp;linkname=PostgreSQL%3A%20CREATE%20PIVOT%20TABLE%20to%20arrange%20Rows%20into%20Columns%20form" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause%2F&amp;linkname=PostgreSQL%3A%20CREATE%20PIVOT%20TABLE%20to%20arrange%20Rows%20into%20Columns%20form" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause%2F&amp;linkname=PostgreSQL%3A%20CREATE%20PIVOT%20TABLE%20to%20arrange%20Rows%20into%20Columns%20form" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause%2F&amp;linkname=PostgreSQL%3A%20CREATE%20PIVOT%20TABLE%20to%20arrange%20Rows%20into%20Columns%20form" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause%2F&amp;linkname=PostgreSQL%3A%20CREATE%20PIVOT%20TABLE%20to%20arrange%20Rows%20into%20Columns%20form" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause%2F&amp;linkname=PostgreSQL%3A%20CREATE%20PIVOT%20TABLE%20to%20arrange%20Rows%20into%20Columns%20form" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause%2F&#038;title=PostgreSQL%3A%20CREATE%20PIVOT%20TABLE%20to%20arrange%20Rows%20into%20Columns%20form" data-a2a-url="https://www.dbrnd.com/2016/09/postgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause/" data-a2a-title="PostgreSQL: CREATE PIVOT TABLE to arrange Rows into Columns form"></a></p><p>In this post, I am going to demonstrate arrangement of the row data to columns which is called a something like Pivot table in PostgreSQL.</p>
<p><strong>What is a Pivot Table?</strong></p>
<blockquote><p><img decoding="async" class="alignright wp-image-3643 size-thumbnail" title="postgresql pivot table" src="https://www.dbrnd.com/wp-content/uploads/2016/09/PostgreSQLPivotTable-150x150.png" alt="postgresql pivot table" width="150" height="150" srcset="https://www.dbrnd.com/wp-content/uploads/2016/09/PostgreSQLPivotTable-150x151.png 150w, https://www.dbrnd.com/wp-content/uploads/2016/09/PostgreSQLPivotTable.png 252w" sizes="(max-width: 150px) 100vw, 150px" />Pivot table is one kind of summary and representation of data, like Microsoft Spreadsheets.</p></blockquote>
<blockquote><p>Pivot table arranges some of row categories into column and also create count and average of that data for better representation.</p></blockquote>
<p>Like Microsoft SQL Server, PostgreSQL doesn’t provide a feature like Pivot Table, We can achieve using SQL Query.</p>
<p><strong>Create a table with sample data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_EmployeePivotTest
(
    EmpName VARCHAR(255)
    ,EmpDeptName VARCHAR(255)
    ,EmpAvgWorkingHours INTEGER
);
 
INSERT INTO tbl_EmployeePivotTest VALUES
('Anvesh','Computer-IT',226)
,('Anvesh','Computer-IT',100)
,('Anvesh','Account',142)
,('Anvesh','Marketing',110)
,('Anvesh','Finance',236)
,('Anvesh','Account',120)
,('Jeeny','Computer-IT',120)
,('Jeeny','Finance',852)
,('Jeeny','Account',326)
,('Jeeny','Marketing',50)
,('Jeeny','Finance',140);</pre><p>
<blockquote><p>Examine this data where two employees with working hour in a different department.<br />
In this data, employee worked more than one time in some of the department.<br />
Now requirement is, to populate different column for different department with total of working hours.</p></blockquote>
<p><strong>SQL Query to PIVOT Table (Using GroupBy Clause):</strong></p><pre class="crayon-plain-tag">SELECT 
     EmpName
    ,SUM(Computer_IT) AS Total_IT
    ,SUM(Account) AS Total_Account
    ,SUM(Marketing) AS Total_Marketing
    ,SUM(Finance) AS Total_Finance
FROM 
(
    SELECT
	EmpName
	,CASE WHEN EmpDeptName = 'Computer-IT' 
		THEN EmpAvgWorkingHours END AS Computer_IT
	,CASE WHEN EmpDeptName = 'Account' 
		THEN EmpAvgWorkingHours END AS Account
	,CASE WHEN EmpDeptName = 'Marketing' 
		THEN EmpAvgWorkingHours END AS Marketing
	,CASE WHEN EmpDeptName = 'Finance' 
		THEN EmpAvgWorkingHours END AS Finance    
    FROM tbl_EmployeePivotTest
) AS T
GROUP BY EmpName;</pre><p>
<strong>The Result:</strong></p><pre class="crayon-plain-tag">empname | total_it | total_account | total_marketing | total_finance
---------+----------+---------------+-----------------+---------------
 Anvesh  |      326 |           262 |             110 |           236
 Jeeny   |      120 |           326 |              50 |           992</pre><p>
In the above result,</p>
<blockquote><p>You can see the result of two employees with total working hour by each department.</p></blockquote>
<blockquote><p>The first step is inner query, in which we have selected employee working hour base on department category.</p>
<p>The Second step is to select this data in the outer query and apply group by clause for SUM ().</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/09/postgresql-create-pivot-table-to-arrange-rows-into-columns-form-using-group-by-clause/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
