Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxedH-0004sc-Ni for pgsql-sql@arkaria.postgresql.org; Tue, 13 Aug 2019 21:47:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hxecI-00084X-4B for pgsql-sql@arkaria.postgresql.org; Tue, 13 Aug 2019 21:46:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxecH-00081T-Oc for pgsql-sql@lists.postgresql.org; Tue, 13 Aug 2019 21:46:05 +0000 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxecE-0001NY-Ug for pgsql-sql@lists.postgresql.org; Tue, 13 Aug 2019 21:46:05 +0000 Received: by mail-pg1-x533.google.com with SMTP id i18so51941856pgl.11 for ; Tue, 13 Aug 2019 14:46:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=TFWthqlwHglUvmOY2FoSJSM1QI5AZwilae/4iE7CXCI=; b=Dx1qYG3gfAfrWbXLbw7gtRpqOKUBjbjzT2+rrnhOMB5nEI2izsfWetfaYQ8QZGyPSN GVnInCnle2C8Wq3yVUfxAHTW8LQQJn63uAlT5SfVTdZmae65YX3NHZSqiqA4CzVdTScd B4tmZsu+FdOhrbke/y6JcM5OPXx1wEk5oGmMV7fm+ZwPmcY2rN+y4+rnsGzzvpo1oMZv LQ2M5tkOvSlhPSBQo3y0+NmkLc7sqfTAEMQmA8nMsjc1pYkahzR6r55lxMcdlgjTpmSQ c5LLeGg/xMnuFzVlux5TO7QuKwAJ8BbWQgoObSsReZUyHvIp3Xya9kVDee5vJSKvOBc+ MkpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=TFWthqlwHglUvmOY2FoSJSM1QI5AZwilae/4iE7CXCI=; b=dFzwYCGIckY8fLeKIYKiUiXRbM7Oh7v76dwFVhlUHHQ4w3bM5Aotoe75ezfjRwywYz MoY9zn38zhQDOXa2YDMu1tZaRBbC818XU4GbBefTHFwDP4HzvxLYUMrhfBSI6mU4SZd9 X8n+5NQEhXMWjIriCfjuJsBwRUNIfKaUKe0pbhVgydzsdn73ZZeo0vTbI/GYwjh3g3lY fIfWqDM9lZtC0WBfOJhYnLvcAfQKoo5Puu63gwOzqw8P/ohZLbQnJk/0wuP6RttIvT3z cVvtsyzm4PIBe0yjVEJz8IfDYVDw79fWMVpkzLlZ82++fPfP+FKar8SSq+8zZp6nSojj SSsw== X-Gm-Message-State: APjAAAXX6TOVw/xRCtmlliV6JIp8PW5t01i2sr1R0LW5W1wN6ngBALyS V6PZeUdQPqZcM4lphHD7HwNQWHQE X-Google-Smtp-Source: APXvYqwDMDdB5Os9jpkq/EnG/BEBI9AJ7rCSAxXx5R/0LqQ7YyoatvCFnkZrcNJtlF07a/Iit8kkZA== X-Received: by 2002:aa7:8f29:: with SMTP id y9mr13355990pfr.27.1565732758866; Tue, 13 Aug 2019 14:45:58 -0700 (PDT) Received: from [10.104.134.41] ([155.100.47.1]) by smtp.gmail.com with ESMTPSA id f14sm109564085pfn.53.2019.08.13.14.45.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Aug 2019 14:45:58 -0700 (PDT) Subject: Re: UPDATE command with FROM clause To: pgsql-sql@lists.postgresql.org References: From: Rob Sargent Message-ID: Date: Tue, 13 Aug 2019 15:45:57 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------359ACD02132022D4EC906064" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------359ACD02132022D4EC906064 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 8/13/19 12:37 PM, JORGE MALDONADO wrote: > Hi, > > I have a query like this: > > UPDATE chartsclub.secc_esp_votar_votos > SET svv_puntos = 0 FROM > (SELECT * FROM chartsclub.secc_esp_votar_votos AS tblVotos WHERE > svv_sva_clave = 114 EXCEPT >        (SELECT DISTINCT ON (svv_fechareg) * FROM > chartsclub.secc_esp_votar_votos AS tblVotos WHERE svv_sva_clave = 114 > ORDER BY svv_fechareg)) > > Will the UPDATE command affect only (all) records generated by the > SELECT clause in the FROM clause? > > I suppose that, if I include a WHERE clause, the condition will be > applied to the records obtained by the SELECT clause in the FROM clause. > Is this correct? > > My goal is to get a set of records from one table and update only such > a set of records. > In this case, the set of records to be updated are those obtained by > the SELECT command in the FROM clause. > As you can see, there is only one table involved but I added an alias > to the SELECT statement in the FROM clause based on what I read in the > documentation. > > Best regards, > Jorge Maldonado > > > Libre de virus. www.avast.com > > > I'm not clear how the value of svv_fechareg affects which rows you don't want to update but I think all you need is something along the lines of UPDATE chartsclub.secc_esp_votar_votos set svv_puntos = 0 where svv_sva_clave =114 --here's where the purpose of svv_fechareg comes into play. if you have an explicit value(s) for it, apply that as "!=" or "not in (value, value)" and svv_fechareg is not null --------------359ACD02132022D4EC906064 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit


On 8/13/19 12:37 PM, JORGE MALDONADO wrote:
Hi,

I have a query like this:

UPDATE chartsclub.secc_esp_votar_votos
SET svv_puntos = 0 FROM
(SELECT * FROM chartsclub.secc_esp_votar_votos AS tblVotos WHERE svv_sva_clave = 114 EXCEPT
       (SELECT DISTINCT ON (svv_fechareg) * FROM chartsclub.secc_esp_votar_votos AS tblVotos WHERE svv_sva_clave = 114 ORDER BY svv_fechareg))

Will the UPDATE command affect only (all) records generated by the SELECT clause in the FROM clause?

I suppose that, if I include a WHERE clause, the condition will be applied to the records obtained by the SELECT clause in the FROM clause. 
Is this correct?

My goal is to get a set of records from one table and update only such a set of records.
In this case, the set of records to be updated are those obtained by the SELECT command in the FROM clause.
As you can see, there is only one table involved but I added an alias to the SELECT statement in the FROM clause based on what I read in the documentation.

Best regards,
Jorge Maldonado

Libre de virus. www.avast.com


I'm not clear how the value of svv_fechareg affects which rows you don't want to update but I think all you need is something along the lines of

UPDATE chartsclub.secc_esp_votar_votos

set svv_puntos = 0

where svv_sva_clave =114

--here's where the purpose of svv_fechareg comes into play. if you have an explicit value(s) for it, apply that as "!=" or "not in (value, value)"

and svv_fechareg is not null 


--------------359ACD02132022D4EC906064--