<?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>Grouping SETS &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/grouping-sets/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>Thu, 18 Aug 2016 17:58:15 +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: Multiple GROUP BY using GROUPING SETS in Single SQL Query</title>
		<link>https://www.dbrnd.com/2016/08/postgresql-multiple-group-by-using-grouping-sets-in-single-sql-query/</link>
					<comments>https://www.dbrnd.com/2016/08/postgresql-multiple-group-by-using-grouping-sets-in-single-sql-query/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 18 Aug 2016 17:58:15 +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[group by]]></category>
		<category><![CDATA[Grouping SETS]]></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=3529</guid>

					<description><![CDATA[In this post, I am sharing one demonstration of PostgreSQL GROUPING SETS. This is very useful for PostgreSQL Database Developers who require to perform multiple GROUP BY in one single query. Now take one example, you want to find the count of Employee based on two columns: Employee Department , Employee Joining Year. For this [&#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%2F08%2Fpostgresql-multiple-group-by-using-grouping-sets-in-single-sql-query%2F&amp;linkname=PostgreSQL%3A%20Multiple%20GROUP%20BY%20using%20GROUPING%20SETS%20in%20Single%20SQL%20Query" 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%2F08%2Fpostgresql-multiple-group-by-using-grouping-sets-in-single-sql-query%2F&amp;linkname=PostgreSQL%3A%20Multiple%20GROUP%20BY%20using%20GROUPING%20SETS%20in%20Single%20SQL%20Query" 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%2F08%2Fpostgresql-multiple-group-by-using-grouping-sets-in-single-sql-query%2F&amp;linkname=PostgreSQL%3A%20Multiple%20GROUP%20BY%20using%20GROUPING%20SETS%20in%20Single%20SQL%20Query" 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%2F08%2Fpostgresql-multiple-group-by-using-grouping-sets-in-single-sql-query%2F&amp;linkname=PostgreSQL%3A%20Multiple%20GROUP%20BY%20using%20GROUPING%20SETS%20in%20Single%20SQL%20Query" 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%2F08%2Fpostgresql-multiple-group-by-using-grouping-sets-in-single-sql-query%2F&amp;linkname=PostgreSQL%3A%20Multiple%20GROUP%20BY%20using%20GROUPING%20SETS%20in%20Single%20SQL%20Query" 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%2F08%2Fpostgresql-multiple-group-by-using-grouping-sets-in-single-sql-query%2F&amp;linkname=PostgreSQL%3A%20Multiple%20GROUP%20BY%20using%20GROUPING%20SETS%20in%20Single%20SQL%20Query" 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%2F08%2Fpostgresql-multiple-group-by-using-grouping-sets-in-single-sql-query%2F&#038;title=PostgreSQL%3A%20Multiple%20GROUP%20BY%20using%20GROUPING%20SETS%20in%20Single%20SQL%20Query" data-a2a-url="https://www.dbrnd.com/2016/08/postgresql-multiple-group-by-using-grouping-sets-in-single-sql-query/" data-a2a-title="PostgreSQL: Multiple GROUP BY using GROUPING SETS in Single SQL Query"></a></p><p>In this post, I am sharing one demonstration of PostgreSQL GROUPING SETS.<br />
This is very useful for PostgreSQL Database Developers who require to perform multiple GROUP BY in one single query. </p>
<blockquote><p>Now take one example, you want to find the count of Employee based on two columns: Employee Department , Employee Joining Year.<br />
For this query, you have to write different two queries and If you want to combine results of both the query, you should use UNION clause. </p></blockquote>
<p>But using GROUPING SETS, we can prepare this result into one single query.</p>
<p><strong>Below is a full demonstration of this:</strong></p>
<p><strong>Create a table with Sample data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_Employees
(
	EmpID INT
	,EmpName CHARACTER VARYING
	,EmpDepartment CHARACTER VARYING
	,JoiningDate TIMESTAMP WITHOUT TIME ZONE
);

 
INSERT INTO tbl_Employees
VALUES 
(1,'Anvesh','Database','2012-09-06')
,(2,'Roy','Animation','2012-10-08')
,(3,'Martin','JAVA','2013-12-25')
,(4,'Eric','JAVA','2014-01-26')
,(5,'Jenny','JAVA','2014-05-20')
,(6,'Kavita','Database','2014-12-12')
,(7,'Marlin','SQL','2015-08-08')
,(8,'Mahesh','PHP','2016-06-16');</pre><p> </p>
<p><strong>Count of Employee, Group By Department column:</strong></p>
<pre class="crayon-plain-tag">SELECT 
	EmpDepartment
	,COUNT(1) AS EmployeeCount
FROM tbl_Employees 
GROUP BY EmpDepartment;</pre> </p>
<p><strong>Count of Employee, Group By Joining Year:</strong></p>
<pre class="crayon-plain-tag">SELECT 
	EXTRACT(YEAR FROM JoiningDate) AS JoiningYear
	,COUNT(1) AS EmployeeCount
FROM tbl_Employees 
GROUP BY EXTRACT(YEAR FROM JoiningDate);</pre> </p>
<p><strong>GROUP BY above two queries using GROUPING SETS:</strong></p>
<pre class="crayon-plain-tag">SELECT 
	EmpDepartment
	,EXTRACT(YEAR FROM JoiningDate) AS JoiningYear
	,COUNT(1) AS EmployeeCount
FROM tbl_Employees
GROUP BY GROUPING SETS (EmpDepartment,EXTRACT(YEAR FROM JoiningDate));</pre> </p>
<p><strong>The Result:</strong></p>
<pre class="crayon-plain-tag">EmpDepartment | JoiningYear | EmployeeCount
---------------------------------------------
 Animation                         1
 Database                          2
 JAVA                              3
 PHP                               1
 SQL                               1
                   2012            2
                   2013            1
                   2014            3
                   2015            1
                   2016            1</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/08/postgresql-multiple-group-by-using-grouping-sets-in-single-sql-query/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
