Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fgQyV-0000I4-Ku for pgsql-general@arkaria.postgresql.org; Fri, 20 Jul 2018 08:41:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fgQyT-0005Ti-EH for pgsql-general@arkaria.postgresql.org; Fri, 20 Jul 2018 08:41:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fgQyT-0005Tb-2f for pgsql-general@lists.postgresql.org; Fri, 20 Jul 2018 08:41:17 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fgQyL-0004Wj-TW for pgsql-general@postgresql.org; Fri, 20 Jul 2018 08:41:15 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 3CAC84105A; Fri, 20 Jul 2018 10:41:06 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by gerste.heinlein-support.de (gerste.heinlein-support.de [91.198.250.173]) (amavisd-new, port 10030) with ESMTP id VPlulxfc7UI4; Fri, 20 Jul 2018 10:41:04 +0200 (CEST) Message-ID: <1532076061.2610.3.camel@cybertec.at> Subject: Re: sql questions From: Laurenz Albe To: hamann.w@t-online.de, pgsql-general@postgresql.org Date: Fri, 20 Jul 2018 10:41:01 +0200 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk hamann.w@t-online.de wrote: > a) I am running some select query > select ... order by .... > Now, I would like to preserver the ordering through further processing by adding a sequence number > Of course I can do: > create temp sequence mseq; > select xx.*, nextval('mseq') as ord from (select ... order by ....) xx; > drop sequence mseq; > Is there a simpler way (avoiding the create/drop parts) A window function would be the best thing: SELECT ..., row_number() OVER (ORDER BY ...) FROM ... > b) can a sql function return the count of affected rows of some query? > create function merge_names(int, int) returns void as > $_$ > update namelinks set nid = $2 where nid = $1; > -- want the affected rows of the above query > delete from names where nid = $1 > -- return result here > $_$ > language sql; You cannot do it in an SQL function. In PL/pgSQL you can use GET DIAGNOSTICS avariable = ROW_COUNT; Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com