<?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>array_agg &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/array_agg/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, 12 May 2017 11:56:34 +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: Use array_agg, to get DISTINCT value for each Column</title>
		<link>https://www.dbrnd.com/2017/05/postgresql-use-array_agg-to-get-distinct-value-for-each-column/</link>
					<comments>https://www.dbrnd.com/2017/05/postgresql-use-array_agg-to-get-distinct-value-for-each-column/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 23 May 2017 16:16:58 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[array_agg]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DISTINCT]]></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>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5285</guid>

					<description><![CDATA[In this post, I am sharing a demonstration to select a distinct value for each column of a table in PostgreSQL. You can get distinct values for each column using array_agg() which returns a result in array format because we are trying to build our result like One to Many relationships. For further data analysis, [&#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%2F2017%2F05%2Fpostgresql-use-array_agg-to-get-distinct-value-for-each-column%2F&amp;linkname=PostgreSQL%3A%20Use%20array_agg%2C%20to%20get%20DISTINCT%20value%20for%20each%20Column" 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%2F2017%2F05%2Fpostgresql-use-array_agg-to-get-distinct-value-for-each-column%2F&amp;linkname=PostgreSQL%3A%20Use%20array_agg%2C%20to%20get%20DISTINCT%20value%20for%20each%20Column" 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%2F2017%2F05%2Fpostgresql-use-array_agg-to-get-distinct-value-for-each-column%2F&amp;linkname=PostgreSQL%3A%20Use%20array_agg%2C%20to%20get%20DISTINCT%20value%20for%20each%20Column" 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%2F2017%2F05%2Fpostgresql-use-array_agg-to-get-distinct-value-for-each-column%2F&amp;linkname=PostgreSQL%3A%20Use%20array_agg%2C%20to%20get%20DISTINCT%20value%20for%20each%20Column" 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%2F2017%2F05%2Fpostgresql-use-array_agg-to-get-distinct-value-for-each-column%2F&amp;linkname=PostgreSQL%3A%20Use%20array_agg%2C%20to%20get%20DISTINCT%20value%20for%20each%20Column" 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%2F2017%2F05%2Fpostgresql-use-array_agg-to-get-distinct-value-for-each-column%2F&amp;linkname=PostgreSQL%3A%20Use%20array_agg%2C%20to%20get%20DISTINCT%20value%20for%20each%20Column" 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%2F2017%2F05%2Fpostgresql-use-array_agg-to-get-distinct-value-for-each-column%2F&#038;title=PostgreSQL%3A%20Use%20array_agg%2C%20to%20get%20DISTINCT%20value%20for%20each%20Column" data-a2a-url="https://www.dbrnd.com/2017/05/postgresql-use-array_agg-to-get-distinct-value-for-each-column/" data-a2a-title="PostgreSQL: Use array_agg, to get DISTINCT value for each Column"></a></p><p>In this post, I am sharing a demonstration to select a distinct value for each column of a table in PostgreSQL. </p>
<p>You can get distinct values for each column using array_agg() which returns a result in array format because we are trying to build our result like One to Many relationships. </p>
<p>For further data analysis, we can use this array result which contains distinct values for a corresponding entity. </p>
<p>Below is a demonstration of this:</p>
<p><strong>Create a table with sample records:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_EmployeeHistory
(
	EmpName text
	,DepartmentName text
	,Weeks text
);

INSERT INTO tbl_EmployeeHistory VALUES 
('Anvesh','Animation','Week 1')
,('Roy','Animation','Week 3')
,('Anvesh','Database','Week 2')
,('Anvesh','Animation','Week 4')
,('Martine','Product Design','Week 2')
,('Anvesh','Database','Week 3')
,('Martine','Animation','Week 1')
,('Martine','Animation','Week 3')
,('Roy','Animation','Week 4')
,('Roy','Database','Week 1')
,('Roy','Product Design','Week 2')
,('Martine','Product Design','Week 3');</pre> </p>
<p><strong>Use array_agg with DISTINCT:</strong></p>
<pre class="crayon-plain-tag">SELECT
	EmpName
	,array_agg(DISTINCT DepartmentName) AS DepartmentName
FROM tbl_EmployeeHistory
GROUP BY EmpName;</pre> </p>
<p><strong>The Result:</strong></p>
<pre class="crayon-plain-tag">empname |            departmentname
---------+---------------------------------------
 Anvesh  | {Animation,Database}
 Martine | {Animation,"Product Design"}
 Roy     | {Animation,Database,"Product Design"}
(3 rows)</pre> </p>
<p><strong>Use array_agg with DISTINCT for multiple columns:</strong></p>
<pre class="crayon-plain-tag">SELECT
	EmpName
	,array_agg(DISTINCT DepartmentName) AS DepartmentName
	,array_agg(DISTINCT Weeks) AS Weeks
FROM tbl_EmployeeHistory
GROUP BY EmpName;</pre> </p>
<p><strong>The Result:</strong></p>
<pre class="crayon-plain-tag">empname |            departmentname             |                 weeks
---------+---------------------------------------+---------------------------------------
 Anvesh  | {Animation,Database}                  | {"Week 1","Week 2","Week 3","Week 4"}
 Martine | {Animation,"Product Design"}          | {"Week 1","Week 2","Week 3"}
 Roy     | {Animation,Database,"Product Design"} | {"Week 1","Week 2","Week 3","Week 4"}
(3 rows)</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/05/postgresql-use-array_agg-to-get-distinct-value-for-each-column/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
