<p style="margin:0px;font-size:12px;font-family:Helvetica">Boa tarde,</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Estou com um problema com o Postgresql que penso que alguém versado em sql poderá resolver com facilidade.</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Numa base de dados dos Censos 2001, tenho várias tabelas, duas das quais chamam-se <c2001_individuos> e c2001_subsecs (relacionadas pelo código da sub-secção estatística)</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Pretendo obter uma tabela com cada freguesia (censos2001.c2001_subsecs.cc_dsg_eu02) do concelho de Tomar (censos2001.c2001_subsecs.cc_dsg_eu02 = 'Tomar'), com a soma da população residente em cada ( sum(censos2001.c2001_individuos.ttr) ).</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Tentei várias coisas, mas sem sucesso.</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">---------------------</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Tentativa 1:</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">select</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs.ff_dsg_eu02,</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>sum(censos2001.c2001_individuos.ttr)</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">from</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs,</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_individuos</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><span class="Apple-tab-span" style="white-space:pre"> </span></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">where</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs.subsec = censos2001.c2001_individuos.subsec</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>group by censos2001.c2001_subsecs.ff_dsg_eu02</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>having censos2001.c2001_subsecs.cc_dsg_eu02 = 'Tomar';</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">ERRO:</p>
<p style="margin:0px;font-size:12px;font-family:Monaco"><span style="font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span></span>ERROR: column "c2001_subsecs.cc_dsg_eu02" must appear in the GROUP BY clause or be used in an aggregate function</p>
<p style="margin:0px;font-size:12px;font-family:Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>(compreendo mas não comsigo resolver)</p>
<p style="margin:0px;font-size:12px;font-family:Monaco;min-height:16px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">---------------------</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Tentativa 2:</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">select</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs.ff_dsg_eu02,</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>sum(censos2001.c2001_individuos.ttr)</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">from</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs,</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_individuos</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><span class="Apple-tab-span" style="white-space:pre"> </span></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">where</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs.cc_dsg_eu02 = (select censos2001.c2001_subsecs.cc_dsg_eu02 from censos2001.c2001_subsecs where censos2001.c2001_subsecs.cc_dsg_eu02 = 'Tomar')</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">AND</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><span class="Apple-tab-span" style="white-space:pre"> </span></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs.subsec = censos2001.c2001_individuos.subsec</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>group by censos2001.c2001_subsecs.ff_dsg_eu02;</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">ERRO:</p>
<p style="margin:0px;font-size:12px;font-family:Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>ERROR: more than one row returned by a subquery used as an expression</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">--------------------------</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Se eu pretendesse obter simplesmente o total da população de Tomar, sei que seria fácil:</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">select</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs.cc_dsg_eu02,</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>sum(censos2001.c2001_individuos.ttr)</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">from</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs,</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_individuos</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><span class="Apple-tab-span" style="white-space:pre"> </span></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">where</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>censos2001.c2001_subsecs.subsec = censos2001.c2001_individuos.subsec</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>group by censos2001.c2001_subsecs.cc_dsg_eu02</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica"><span class="Apple-tab-span" style="white-space:pre"> </span>having censos2001.c2001_subsecs.cc_dsg_eu02 = 'Tomar';</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Mas não é bem isto…</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Sei que me está a falhar algo simples, mas o quê?</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Isto da pouca prática com SQL tem destas coisas.</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Obrigado.</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">Abraços,</p>
<p style="margin:0px;font-size:12px;font-family:Helvetica">António</p>