Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kJMPx-0008P6-Hy for pgsql-general@arkaria.postgresql.org; Fri, 18 Sep 2020 19:51:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kJMPv-0007ZN-Ov for pgsql-general@arkaria.postgresql.org; Fri, 18 Sep 2020 19:51:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kJMPv-0007ZG-F5 for pgsql-general@lists.postgresql.org; Fri, 18 Sep 2020 19:51:35 +0000 Received: from mail-io1-xd2b.google.com ([2607:f8b0:4864:20::d2b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kJMPo-00032X-Ch for pgsql-general@lists.postgresql.org; Fri, 18 Sep 2020 19:51:35 +0000 Received: by mail-io1-xd2b.google.com with SMTP id r9so8284724ioa.2 for ; Fri, 18 Sep 2020 12:51:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=czin4etb+zD0Fjg8IkgZrmKk12FigFnWdosPjBTYZI8=; b=fAT5JTJgunZ2c09xo4JkPkO2A/apoNSm4vqQ+IYzwSUIxWfVpibOJugUcfAnWbtMNv w9FYHm/htUUL7LrjtQdlrHv+NYT0pbyT/eervz5ZmkSUrPJplOhCLTnb9Q58MVw2MDnV EnuRWooOHAxSMujfAjJz16LkGJEFIxSHs6DoDwwz/L3m3UQAVZEcuNvH92rGhcJLdbP7 gRgPZHaVLwjp3Mipqyzmn90zkX6fpJl7yhQOhWDVDQlB1sHeUNHWzfIuLJJ0XQgcolTD ZfL4NarQcLOwwPKnZNi7hEQIChzmxs3lOyqDfpcTMZ6zgRll56ojXwS/BHQ9l5lIE9lw I/7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=czin4etb+zD0Fjg8IkgZrmKk12FigFnWdosPjBTYZI8=; b=czrZS4dmRfDyFChl5S4IXvOO0UXbK6SHy7yvxkpOExTdeEaeGkbAgwnWdod4Gxbd3G 8NYQ/7/fhX8r9Z+zTMSqVFDSj8pMTEEk4eaxePeXHfcttyCsal7Y/HbavIIFPmfJQNJp sgs6MSrROxGhwDmAlTCNJvgrayGA0rgnFY5ODc84evRryPiDgDwgM7eZMgELE0MSikdT 51EGzrgkfCbxBHTDDW8laK8mH8AY6SiQxINO8zOwh7E2WVvdvOA3KjYpAa9qSsSohly7 ZaR0k9PfWuMNHJRlTAjOguemGBpUMPCO5ug1SKftS93iZcXA4l6G2b0m00r1Q7vHQCCW 3mgA== X-Gm-Message-State: AOAM5333hg9u2QI7z8xOuf1XkHrrgTFXAopW/DEEI2MfsCK1rFTybff7 /+DUQwyIKvkWKa6XrRUTztDYAvAY8Yg= X-Google-Smtp-Source: ABdhPJzeRXRhNoSM3rGrKCD75YUcfGAdXmPeD+d7pZn4YngfU07QQKQFqc1HqCrUquifha4QobELJA== X-Received: by 2002:a05:6602:154e:: with SMTP id h14mr28891627iow.17.1600458686294; Fri, 18 Sep 2020 12:51:26 -0700 (PDT) Received: from [172.16.59.4] ([208.139.204.134]) by smtp.gmail.com with ESMTPSA id d8sm2251084ilu.2.2020.09.18.12.51.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Sep 2020 12:51:25 -0700 (PDT) From: Rob Sargent Message-Id: <1C9CA252-1689-4F69-BC66-CDCDFE46FE87@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_51BFBD41-B76C-45BF-92CC-3B856DDD4BFA" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.1\)) Subject: Re: How to write such a query Date: Fri, 18 Sep 2020 13:51:51 -0600 In-Reply-To: Cc: Ron , Ken Tanzer To: "pgsql-generallists.postgresql.org" References: <912dd33c-c837-a226-111f-413fcb7321c1@aklaver.com> X-Mailer: Apple Mail (2.3608.120.23.2.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_51BFBD41-B76C-45BF-92CC-3B856DDD4BFA Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Sep 18, 2020, at 1:45 PM, Ken Tanzer wrote: >=20 > > How to find what the primary key (or UNIQUE identifier) value is > > for row 5 in the recordset? >=20 > You're missing the point: as mentioned before, there is no "row 5". To=20= > update the 5th record that you've fetched, you increment a counter = each time=20 > you fetch a row, and when you read #5, do an UPDATE X SET field1 =3D = 'blarg'=20 > WHERE id =3D ; >=20 >=20 > It seems worth mentioning for benefit of the OPs question that there = _is_ a way to get a row number within a result set. Understanding and = making good use of that is an additional matter. >=20 > SELECT X.field1, Y.field2,row_number() OVER () from X, Y WHERE X.id =3D= Y.id -- ORDER BY ____? >=20 > That row number is going to depend on the order of the query, so it = might or might not have any meaning. But if you queried with a primary = key and a row number, you could then tie the two together and make an = update based on that. If =E2=80=9Crow 5=E2=80=9D as seen by the OP has no distinguishing = characteristic directing OP to edit that tuple then he=E2=80=99s in a = world of hurt, well beyond the reach of anyone here.= --Apple-Mail=_51BFBD41-B76C-45BF-92CC-3B856DDD4BFA Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

On Sep 18, 2020, at 1:45 PM, Ken Tanzer <ken.tanzer@gmail.com> wrote:

> How to find what the primary key = (or UNIQUE identifier) value is
> for row 5 in the recordset?

You're missing the point: as mentioned before, there is no "row 5". To =
update the 5th record that you've fetched, you increment a counter each = time
you fetch a row, and when you read #5, do an UPDATE X SET field1 =3D = 'blarg'
WHERE id =3D <thekeyvalue>;


It seems worth mentioning for benefit = of the OPs question that there _is_ a way to get a row number = within a result set.  Understanding and making good use of = that is an additional matter.

SELECT X.field1, Y.field2,row_number() OVER ()  from X, Y WHERE X.id =3D Y.id -- ORDER BY ____?

That row number is going = to depend on the order of the query, so it might or might not have any = meaning.  But if you queried with a primary key and a row = number, you could then tie the two together and make an update based on = that.

If =E2=80=9Crow 5=E2=80=9D as seen by the OP has no = distinguishing characteristic directing OP to edit that tuple then = he=E2=80=99s in a world of hurt, well beyond the reach of anyone = here.
= --Apple-Mail=_51BFBD41-B76C-45BF-92CC-3B856DDD4BFA--