<?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>LATERAL &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/lateral/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, 25 Aug 2016 17:43:16 +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: 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 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>
	</channel>
</rss>
