<?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>Phaneronsoft &#187; PostgreSQL</title>
	<atom:link href="http://www.phaneronsoft.com/phaneronsoft/category/sql/postgresql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phaneronsoft.com/phaneronsoft</link>
	<description>Desenvolvimento Web</description>
	<lastBuildDate>Sat, 28 Aug 2010 00:11:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Criar função no PostgreSQL GROUP_CONCAT equivalente ao GROUP_CONCAT MySQL</title>
		<link>http://www.phaneronsoft.com/phaneronsoft/2009/09/17/criar-funcao-no-postgresql-group_concat-equivalente-ao-group_concat-mysql/</link>
		<comments>http://www.phaneronsoft.com/phaneronsoft/2009/09/17/criar-funcao-no-postgresql-group_concat-equivalente-ao-group_concat-mysql/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 12:38:40 +0000</pubDate>
		<dc:creator>Marcelo Korjenioski</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://www.phaneronsoft.com/phaneronsoft/?p=370</guid>
		<description><![CDATA[Hoje estava precisando concatenar dados de um campo e descobri que no PostgreSQL não existe uma função equivalente ao group_concat do MySQL. Exemplo do MySQL select GROUP_CONCAT(nome ORDER BY nome DESC SEPARATOR &#039;,&#039;) as nomes from usuarios Então procurando na web encontrei esta função. MS SQL to Postgre SQL create aggregate array_accum ( sfunc = [...]]]></description>
			<content:encoded><![CDATA[<p>Hoje estava precisando concatenar dados de um campo e descobri que no PostgreSQL não existe uma função equivalente ao group_concat do MySQL.</p>
<p>Exemplo do MySQL</p>
<pre class="brush: sql">
select GROUP_CONCAT(nome ORDER BY nome DESC SEPARATOR &#039;,&#039;) as nomes from usuarios
</pre>
<p>Então procurando na web encontrei esta função.</p>
<p><a href="http://mssql-to-postgresql.blogspot.com/2007/12/cool-groupconcat.html">MS SQL to Postgre SQL</a></p>
<pre class="brush: sql">

create aggregate array_accum (
sfunc = array_append,
basetype = anyelement,
stype = anyarray,
initcond = &#039;{}&#039;
);

CREATE OR REPLACE FUNCTION _group_concat(text, text)
RETURNS text AS $$
SELECT CASE
WHEN $2 IS NULL THEN $1
WHEN $1 IS NULL THEN $2
ELSE $1 operator(pg_catalog.||) &#039;,&#039; operator(pg_catalog.||) $2
END
$$ IMMUTABLE LANGUAGE SQL;

CREATE AGGREGATE group_concat (
BASETYPE = text,
SFUNC = _group_concat,
STYPE = text
);
</pre>
<p>Para executar a função basta usar.</p>
<pre class="brush: sql">
select group_concat(distinct nome) as nomes from usuarios
</pre>
<p>Caso tenha alguma duvida ou suges­tão deixe um comen­tá­rio que terei pra­zer em aju­dar.<br />
Comen­tá­rios são bem vin­dos assim posso melho­rar a qua­li­dade dos tuto­ri­ais aqui apresentados.</p>
<p>É isso pes­soal uma ótima semana e até o próximo post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phaneronsoft.com/phaneronsoft/2009/09/17/criar-funcao-no-postgresql-group_concat-equivalente-ao-group_concat-mysql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
