<?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>group by &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/group-by/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>Fri, 08 Feb 2019 18:51:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>Database Design: Please do not use DISTINCT</title>
		<link>https://www.dbrnd.com/2019/02/database-design-please-do-not-use-distinct-keyword-remove-duplicate-data/</link>
					<comments>https://www.dbrnd.com/2019/02/database-design-please-do-not-use-distinct-keyword-remove-duplicate-data/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 04 Feb 2019 16:16:09 +0000</pubDate>
				<category><![CDATA[Database Designing]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database concept]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[database standards]]></category>
		<category><![CDATA[Database Theory]]></category>
		<category><![CDATA[database topic]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DISTINCT]]></category>
		<category><![CDATA[group by]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[RDBMS]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4130</guid>

					<description><![CDATA[<div><img width="336" height="336" src="https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct.jpg" class="attachment-post-image size-post-image wp-post-image" alt="database design angry distinct" decoding="async" fetchpriority="high" srcset="https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct.jpg 336w, https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct-150x150.jpg 150w, https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct-300x300.jpg 300w" sizes="(max-width: 336px) 100vw, 336px" /></div>I am telling you personally that I hate the use of DISTINCT. DISTINCT used by those people, who are not sure about their data set or SELECT statement or JOINS. Whenever I get any query with DISTINCT, immediately I suggest to remove it. I asked to many developers that, does it makes sense to use [&#8230;]]]></description>
										<content:encoded><![CDATA[<div><img width="336" height="336" src="https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct.jpg" class="attachment-post-image size-post-image wp-post-image" alt="database design angry distinct" decoding="async" srcset="https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct.jpg 336w, https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct-150x150.jpg 150w, https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct-300x300.jpg 300w" sizes="(max-width: 336px) 100vw, 336px" /></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F02%2Fdatabase-design-please-do-not-use-distinct-keyword-remove-duplicate-data%2F&amp;linkname=Database%20Design%3A%20Please%20do%20not%20use%20DISTINCT" 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%2F2019%2F02%2Fdatabase-design-please-do-not-use-distinct-keyword-remove-duplicate-data%2F&amp;linkname=Database%20Design%3A%20Please%20do%20not%20use%20DISTINCT" 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%2F2019%2F02%2Fdatabase-design-please-do-not-use-distinct-keyword-remove-duplicate-data%2F&amp;linkname=Database%20Design%3A%20Please%20do%20not%20use%20DISTINCT" 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%2F2019%2F02%2Fdatabase-design-please-do-not-use-distinct-keyword-remove-duplicate-data%2F&amp;linkname=Database%20Design%3A%20Please%20do%20not%20use%20DISTINCT" 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%2F2019%2F02%2Fdatabase-design-please-do-not-use-distinct-keyword-remove-duplicate-data%2F&amp;linkname=Database%20Design%3A%20Please%20do%20not%20use%20DISTINCT" 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%2F2019%2F02%2Fdatabase-design-please-do-not-use-distinct-keyword-remove-duplicate-data%2F&amp;linkname=Database%20Design%3A%20Please%20do%20not%20use%20DISTINCT" 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%2F2019%2F02%2Fdatabase-design-please-do-not-use-distinct-keyword-remove-duplicate-data%2F&#038;title=Database%20Design%3A%20Please%20do%20not%20use%20DISTINCT" data-a2a-url="https://www.dbrnd.com/2019/02/database-design-please-do-not-use-distinct-keyword-remove-duplicate-data/" data-a2a-title="Database Design: Please do not use DISTINCT"></a></p><p><strong><img decoding="async" class="alignright wp-image-4137 size-medium" title="database design angry distinct" src="https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct-300x300.jpg" alt="database design angry distinct" width="300" height="300" srcset="https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct-300x300.jpg 300w, https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct-150x150.jpg 150w, https://www.dbrnd.com/wp-content/uploads/2016/12/DatabaseDesignAngryDistinct.jpg 336w" sizes="(max-width: 300px) 100vw, 300px" />I am telling you personally that I hate the use of DISTINCT.</strong></p>
<blockquote><p>DISTINCT used by those people, who are not sure about their data set or SELECT statement or JOINS.</p></blockquote>
<p><strong>Whenever I get any query with DISTINCT, immediately I suggest to remove it. </strong></p>
<blockquote><p>I asked to many developers that, does it makes sense to use DISTINCT or do we have any alternative?<br />
Second, I asked that Why are you required to use DISTINCT.</p></blockquote>
<p><strong>Guys, what are you thinking? What was that answer?</strong></p>
<blockquote><p>Very simple, they keep quiet for a minute and were telling like: we are using to avoid duplicate result set.</p></blockquote>
<blockquote><p>This is not the correct answer for the use of DISTINCT.</p></blockquote>
<blockquote><p>Actually, most of developers are using DISTINCT because they want to hide their mistakes. They are writing incorrect joins and hiding duplication of data.</p></blockquote>
<blockquote><p>This is not only for database developer, DBA is a same responsible for this. The DBA must stop the duplication of data.</p></blockquote>
<blockquote><p><em>The DBA should give the answer that how duplicates data inserted in the database and who is responsible for that duplication.</em></p></blockquote>
<p><strong>Do you think, the use of DISTINCT is good for query performance?</strong></p>
<blockquote><p>My answer is simply no. Why, because it fetches unnecessary duplicate data and returns unique from it.</p></blockquote>
<blockquote><p>You should know the logical sequence of SQL Query execution. In the logical sequence, DISTINCT execute after SELECT means It checks all the data first and after that It avoids the duplicate data.</p></blockquote>
<p><strong>As I have raised a question for DISTINCT, few developers asked me to use GROUP BY instead of DISTINCT.</strong></p>
<blockquote><p>My simple answer was, Please do not use anything to remove duplicates from your result set.</p></blockquote>
<blockquote><p>Explicitly, you should remove the duplicate data and then also If you require to count few unique master group data, you can use GROUP BY instead of DISTINCT.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/02/database-design-please-do-not-use-distinct-keyword-remove-duplicate-data/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Explain Group By ROLLUP with an example</title>
		<link>https://www.dbrnd.com/2018/04/postgresql-explain-group-by-rollup-with-an-example/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 22 Apr 2018 16:16:19 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Data Analytics]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[group by]]></category>
		<category><![CDATA[GROUP BY ROLLUP]]></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 Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[ROLLUP]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7279</guid>

					<description><![CDATA[In this post, I am sharing a demonstration of PostgreSQL GROUP BY ROLLUP which we are using for data analytics purpose. The data analytics is a huge area and there are hundreds of tools available in the market. OLTP System like PostgreSQL is not made for Data Analytics but supports medium level of data analytics [&#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%2F2018%2F04%2Fpostgresql-explain-group-by-rollup-with-an-example%2F&amp;linkname=PostgreSQL%3A%20Explain%20Group%20By%20ROLLUP%20with%20an%20example" 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%2F2018%2F04%2Fpostgresql-explain-group-by-rollup-with-an-example%2F&amp;linkname=PostgreSQL%3A%20Explain%20Group%20By%20ROLLUP%20with%20an%20example" 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%2F2018%2F04%2Fpostgresql-explain-group-by-rollup-with-an-example%2F&amp;linkname=PostgreSQL%3A%20Explain%20Group%20By%20ROLLUP%20with%20an%20example" 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%2F2018%2F04%2Fpostgresql-explain-group-by-rollup-with-an-example%2F&amp;linkname=PostgreSQL%3A%20Explain%20Group%20By%20ROLLUP%20with%20an%20example" 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%2F2018%2F04%2Fpostgresql-explain-group-by-rollup-with-an-example%2F&amp;linkname=PostgreSQL%3A%20Explain%20Group%20By%20ROLLUP%20with%20an%20example" 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%2F2018%2F04%2Fpostgresql-explain-group-by-rollup-with-an-example%2F&amp;linkname=PostgreSQL%3A%20Explain%20Group%20By%20ROLLUP%20with%20an%20example" 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%2F2018%2F04%2Fpostgresql-explain-group-by-rollup-with-an-example%2F&#038;title=PostgreSQL%3A%20Explain%20Group%20By%20ROLLUP%20with%20an%20example" data-a2a-url="https://www.dbrnd.com/2018/04/postgresql-explain-group-by-rollup-with-an-example/" data-a2a-title="PostgreSQL: Explain Group By ROLLUP with an example"></a></p><p>In this post, I am sharing a demonstration of PostgreSQL GROUP BY ROLLUP which we are using for data analytics purpose.</p>
<p>The data analytics is a huge area and there are hundreds of tools available in the market. OLTP System like PostgreSQL is not made for Data Analytics but supports medium level of data analytics on real-time data.</p>
<p>ROLLUP is generally used for analysis over hierarchical data like generating sub-total and grand-total.</p>
<p>Check the below example:</p>
<p><strong>Create a table with sample data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_ProductSales (ID INT, Product_Category CHARACTER VARYING, Product_Name CHARACTER VARYING, TotalSales INT);

INSERT INTO tbl_ProductSales 
VALUES 
(1,'Game','Mobo Game',200),(2,'Game','PKO Game',400)
,(3,'Fashion','Shirt',500),(4,'Fashion','Shorts',100);</pre><p>
<strong>Check the ROLLUP result:</strong></p><pre class="crayon-plain-tag">SELECT 
    Product_Category
    ,Product_Name
    ,SUM(TotalSales) AS TotalSales
FROM tbl_ProductSales
GROUP BY ROLLUP (Product_Category, Product_Name)
ORDER BY Product_Category, Product_Name</pre><p>
<img loading="lazy" decoding="async" class="alignnone size-full wp-image-7281" src="https://www.dbrnd.com/wp-content/uploads/2018/03/PostgreSQL-Rollup.png" alt="PostgreSQL Rollup" width="765" height="260" srcset="https://www.dbrnd.com/wp-content/uploads/2018/03/PostgreSQL-Rollup.png 765w, https://www.dbrnd.com/wp-content/uploads/2018/03/PostgreSQL-Rollup-679x231.png 679w" sizes="auto, (max-width: 765px) 100vw, 765px" /></p>
<blockquote class="wp-embedded-content" data-secret="Eeq9VE2KPC"><p><a href="https://www.dbrnd.com/2016/08/postgresql-multiple-group-by-using-grouping-sets-in-single-sql-query/">PostgreSQL: Multiple GROUP BY using GROUPING SETS in Single SQL Query</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/08/postgresql-multiple-group-by-using-grouping-sets-in-single-sql-query/embed/#?secret=Eeq9VE2KPC" data-secret="Eeq9VE2KPC" width="600" height="338" title="&#8220;PostgreSQL: Multiple GROUP BY using GROUPING SETS in Single SQL Query&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>MySQL: How to get Max Value for each group?</title>
		<link>https://www.dbrnd.com/2016/10/mysql-how-to-get-max-value-for-each-group/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 01 Oct 2016 16:16:47 +0000</pubDate>
				<category><![CDATA[MySQL]]></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[max value]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=2126</guid>

					<description><![CDATA[In this post, I am giving you a full demonstration on finding max value for each group of records in MySQL. Today morning, I was working on one report and requirement is to find max value records for each particular group of data. I know, You guys are thinking that this is a very simple [&#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%2F10%2Fmysql-how-to-get-max-value-for-each-group%2F&amp;linkname=MySQL%3A%20How%20to%20get%20Max%20Value%20for%20each%20group%3F" 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%2F10%2Fmysql-how-to-get-max-value-for-each-group%2F&amp;linkname=MySQL%3A%20How%20to%20get%20Max%20Value%20for%20each%20group%3F" 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%2F10%2Fmysql-how-to-get-max-value-for-each-group%2F&amp;linkname=MySQL%3A%20How%20to%20get%20Max%20Value%20for%20each%20group%3F" 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%2F10%2Fmysql-how-to-get-max-value-for-each-group%2F&amp;linkname=MySQL%3A%20How%20to%20get%20Max%20Value%20for%20each%20group%3F" 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%2F10%2Fmysql-how-to-get-max-value-for-each-group%2F&amp;linkname=MySQL%3A%20How%20to%20get%20Max%20Value%20for%20each%20group%3F" 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%2F10%2Fmysql-how-to-get-max-value-for-each-group%2F&amp;linkname=MySQL%3A%20How%20to%20get%20Max%20Value%20for%20each%20group%3F" 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%2F10%2Fmysql-how-to-get-max-value-for-each-group%2F&#038;title=MySQL%3A%20How%20to%20get%20Max%20Value%20for%20each%20group%3F" data-a2a-url="https://www.dbrnd.com/2016/10/mysql-how-to-get-max-value-for-each-group/" data-a2a-title="MySQL: How to get Max Value for each group?"></a></p><p>In this post, I am giving you a full demonstration on finding max value for each group of records in MySQL.</p>
<p>Today morning, I was working on one report and requirement is to find max value records for each particular group of data.</p>
<blockquote><p>I know, You guys are thinking that this is a very simple solution using MAX() and GROUP BY clause.<br />
<strong>But many of database developers have always questioned about:</strong> Why we cannot add any other columns in SELECT list which are not part of aggregation or the GROUP BY clause?</p></blockquote>
<p>Below is a one solution, you can find MAX record without using GROUP BY clause and you can also add other columns in the SELECT list. </p>
<p><strong>First, Create a table with sample data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_EmployeeDetails
(
	EmpID INTEGER PRIMARY KEY 
    ,EmpName VARCHAR(50)
    ,EmpSalary BIGINT
    ,DepartmentName VARCHAR(50)
);

INSERT INTO tbl_EmployeeDetails
VALUES 
(1,'Anvesh',80000,'Sales')
,(2,'Neevan',90000,'Sales')
,(3,'Jenny',50000,'Production')
,(4,'Roy',60000,'Production')
,(5,'Martin',30000,'Research')
,(6,'Mahi',85000,'Research')
,(7,'Kruti',45000,'Research')
,(8,'Manish',75000,'Research');</pre><p>
<strong>Find max salary values for each department:</strong></p><pre class="crayon-plain-tag">SELECT ED1.*
FROM tbl_EmployeeDetails ED1                   
LEFT JOIN tbl_EmployeeDetails ED2       
  ON ED1.DepartmentName = ED2.DepartmentName 
  AND ED1.EmpSalary &lt; ED2.EmpSalary
WHERE ED2.EmpSalary IS NULL</pre><p>
<strong>The Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2016/03/MySQLMaxValueForEachGroup.png" rel="attachment wp-att-2128"><img loading="lazy" decoding="async" class="alignnone wp-image-2128 size-full" title="MySQL Max Value For Each Group" src="https://www.dbrnd.com/wp-content/uploads/2016/03/MySQLMaxValueForEachGroup.png" alt="MySQL Max Value For Each Group" width="285" height="94" srcset="https://www.dbrnd.com/wp-content/uploads/2016/03/MySQLMaxValueForEachGroup.png 285w, https://www.dbrnd.com/wp-content/uploads/2016/03/MySQLMaxValueForEachGroup-150x49.png 150w" sizes="auto, (max-width: 285px) 100vw, 285px" /></a></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PostgreSQL: Optimized way to get first Record per each GROUP (using DISTINCT ON, LATERAL)</title>
		<link>https://www.dbrnd.com/2016/08/postgresql-optimized-way-to-get-first-record-per-each-group-using-distinct-on-lateral-sub-queries/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 25 Aug 2016 17:43:16 +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[DISTINCT ON]]></category>
		<category><![CDATA[group by]]></category>
		<category><![CDATA[LATERAL]]></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=3557</guid>

					<description><![CDATA[Dont you think, this is a very common requirement for most of the Database Developer. Yes, MySQL Developer can visit this article. Find top N records for each group in MySQL In PostgreSQL, We can get a first record for each GROUP using different options like: Using DISTINCT ON Using LATERAL CTE with ROW_NUMBER() CTE [&#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-optimized-way-to-get-first-record-per-each-group-using-distinct-on-lateral-sub-queries%2F&amp;linkname=PostgreSQL%3A%20Optimized%20way%20to%20get%20first%20Record%20per%20each%20GROUP%20%28using%20DISTINCT%20ON%2C%20LATERAL%29" 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-optimized-way-to-get-first-record-per-each-group-using-distinct-on-lateral-sub-queries%2F&amp;linkname=PostgreSQL%3A%20Optimized%20way%20to%20get%20first%20Record%20per%20each%20GROUP%20%28using%20DISTINCT%20ON%2C%20LATERAL%29" 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-optimized-way-to-get-first-record-per-each-group-using-distinct-on-lateral-sub-queries%2F&amp;linkname=PostgreSQL%3A%20Optimized%20way%20to%20get%20first%20Record%20per%20each%20GROUP%20%28using%20DISTINCT%20ON%2C%20LATERAL%29" 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-optimized-way-to-get-first-record-per-each-group-using-distinct-on-lateral-sub-queries%2F&amp;linkname=PostgreSQL%3A%20Optimized%20way%20to%20get%20first%20Record%20per%20each%20GROUP%20%28using%20DISTINCT%20ON%2C%20LATERAL%29" 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-optimized-way-to-get-first-record-per-each-group-using-distinct-on-lateral-sub-queries%2F&amp;linkname=PostgreSQL%3A%20Optimized%20way%20to%20get%20first%20Record%20per%20each%20GROUP%20%28using%20DISTINCT%20ON%2C%20LATERAL%29" 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-optimized-way-to-get-first-record-per-each-group-using-distinct-on-lateral-sub-queries%2F&amp;linkname=PostgreSQL%3A%20Optimized%20way%20to%20get%20first%20Record%20per%20each%20GROUP%20%28using%20DISTINCT%20ON%2C%20LATERAL%29" 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-optimized-way-to-get-first-record-per-each-group-using-distinct-on-lateral-sub-queries%2F&#038;title=PostgreSQL%3A%20Optimized%20way%20to%20get%20first%20Record%20per%20each%20GROUP%20%28using%20DISTINCT%20ON%2C%20LATERAL%29" data-a2a-url="https://www.dbrnd.com/2016/08/postgresql-optimized-way-to-get-first-record-per-each-group-using-distinct-on-lateral-sub-queries/" data-a2a-title="PostgreSQL: Optimized way to get first Record per each GROUP (using DISTINCT ON, LATERAL)"></a></p><p>Dont you think, this is a very common requirement for most of the Database Developer.</p>
<p>Yes, MySQL Developer can visit this article.</p>
<blockquote data-secret="xgfpodNpBa" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2015/08/find-top-n-records-for-each-group-in-mysql/">Find top N records for each group in MySQL</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/08/find-top-n-records-for-each-group-in-mysql/embed/#?secret=xgfpodNpBa" data-secret="xgfpodNpBa" width="600" height="338" title="&#8220;Find top N records for each group in MySQL&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>In PostgreSQL, We can get a first record for each GROUP using different options like:</p>
<ul>
<li>Using DISTINCT ON</li>
<li>Using LATERAL</li>
<li>CTE with ROW_NUMBER()</li>
<li>CTE with LATERAL</li>
<li>Subquery with ROW_NUMBER()</li>
<li>Using array_agg()</li>
</ul>
<p>In this demonstration, I am going to give two optimized solutions to get one record per each GROUP.</p>
<p><strong>Create a table with Sample records:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_Employees
(
	EmpID INT
	,EmpName CHARACTER VARYING 	
	,EmpDepartment CHARACTER VARYING
	,EmpSalary INT
);

INSERT INTO tbl_Employees
VALUES 
(1,'Anvesh','Database',90000)
,(2,'Jenny','JAVA',65000)
,(3,'Martin','PHP',85000)
,(4,'Roy','PHP',94000)
,(5,'Eric','PHP',70000)
,(6,'Rajesh','Animation',50000)
,(7,'Mahi','Database',40000)
,(8,'Sofia','JAVA',60000);</pre><p>
<strong>First Solution using DISTINCT ON:</strong><br />
As per the Postgres official document,</p>
<blockquote><p>The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s).<br />
The ORDER BY clause will normally contain additional expression(s) that determine the desired precedence of rows within each DISTINCT ON group.</p></blockquote>
<pre class="crayon-plain-tag">SELECT DISTINCT ON (EmpDepartment) *       
FROM   tbl_Employees
ORDER  BY EmpDepartment
	,EmpSalary DESC;</pre>
<strong>Second Solution using LATERAL:</strong><br />
A LATERAL allows sub-queries to reference columns provided by preceding FROM items.<br />
<pre class="crayon-plain-tag">SELECT DISTINCT T.*
FROM   tbl_Employees e
,LATERAL 
(
   SELECT *
   FROM   tbl_Employees
   WHERE  EmpDepartment = e.EmpDepartment
   ORDER  BY EmpSalary DESC
   LIMIT  1
) AS T;</pre>
<p><strong>The Result of both the solution :</strong><br />
<pre class="crayon-plain-tag">empid | empname | empdepartment | empsalary
-------+---------+---------------+-----------
     6 | Rajesh  | Animation     |     50000
     1 | Anvesh  | Database      |     90000
     2 | Jenny   | JAVA          |     65000
     4 | Roy     | PHP           |     94000</pre>
]]></content:encoded>
					
		
		
			</item>
		<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>
