Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vFyPr-004ona-FQ for pgsql-general@arkaria.postgresql.org; Mon, 03 Nov 2025 17:32:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vFyPp-006mmz-Rq for pgsql-general@arkaria.postgresql.org; Mon, 03 Nov 2025 17:32:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vFyPp-006mkf-GI for pgsql-general@lists.postgresql.org; Mon, 03 Nov 2025 17:32:24 +0000 Received: from mail-ej1-x62a.google.com ([2a00:1450:4864:20::62a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vFyPm-005mjd-06 for pgsql-general@lists.postgresql.org; Mon, 03 Nov 2025 17:32:23 +0000 Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-b4736e043f9so42037866b.0 for ; Mon, 03 Nov 2025 09:32:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762191141; x=1762795941; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=73JqDsgWduY0zxzv267bDFL7QG+xXGpPYW/fCBqdEFY=; b=XmbDKau9hHAxUd2FWGK52eWyRZZGt1wIGTVaQ44Jl5O3s7ZVafi/02EqGGKUDvZUHf d1DZMpv6lxW8AgMCTkoskPjdvadjwxoOIqI6uJz4j5QLRI/NXJDtV9Bxmj9clTVw57qo 50UPc1j6+meKGWLaBf23LEI6GcU5WvZAFi5neRCxt70rk9gAcaq/pegYbpOWw9iuqckk bx3gE6Qs+5fzZJMPeWispaQzzZM/IfhcJ3Tl+ppABK29Y3DRGoK9mdF97R6WhHH00XiG b0qPKdH6Sb2GGJtuslbpeH3Zhrcw9IjeoSTLth0b7yXNt/soYIVIQfLvA9xxzJJzo0K1 nUWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762191141; x=1762795941; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=73JqDsgWduY0zxzv267bDFL7QG+xXGpPYW/fCBqdEFY=; b=FI07YaW7QSFN+5X/J1sckdo93PpzMBzItqV358f5a0/AW0QiXCgadrjmn7WY+3Ay+c Dpm5tOFqSVaohyTEB/Jl1EXjt/ELLISl9QqAyJrHS09ySdOHYQ58IZpzJR0Bz4ILmXZJ yz6Ajtlybx37pxl4Uu/88haWQvIB1XCjTcwLL0+Fzw9VxH1EoWUlFoqJLXw+7jPTW1wH 0kBMAi3XHmaks+z1DfNlN8oBMwcwmTwt5U+PXXzczDOiix9frprysXSc19Ls9f/fRHim CQSCuArdpQlXRRc6TKH8MPK1fNdXkCWdDzOQMfhMwH94Yi4LWxVlWuGpJEkg/W/0SJdE wctw== X-Gm-Message-State: AOJu0YyxzZS49u69P2bt/s01JNvAJ7UAsyQNuCbNtYOBNgDau9rX6tik Kewky/M9o5r42BzIcoX9pHPDcR20WnJdqQ+3Bk3Vi8dTDzskKISWPh4r24K9Fwumh6qRNRqfRX3 cclZ43fSgmFdBEbfEEKe2Fva2Get2gumcyQ== X-Gm-Gg: ASbGncvCEd4qjb2DkArCDeij+VMFrTRZ5OxhvZBEmFtmtBPdhJYxV2qJshoalCoZxmd VNUsKNsHHHBox48oMDRyc9/DuDR9+7Cg9FsCqcOv8QPJVgcINfhLHj9Mx+wiFzTMXsixDYSBFUW 1FuPlMQpBv9+xiqKtx+aDqRkxBc4tcq9UcuP9z9eMC7mPBFlqEJUea112d7tx/8e5Rvk7GnlUqF 5QY5uNG8Vh78mYiFdh3yOpeFB+YtJhJrWgTNDAd3b+libAEbJ4Pu6MaoTa2utt39E9pbnEq X-Google-Smtp-Source: AGHT+IEPVrHFJcmRWpVDPnCXtQ+sUDj0JxMgbMJ0HePaxUkyzbumyY+tqKO9bQaymQMETWy4EAMFfoez4W/LyC7VIxY= X-Received: by 2002:a17:907:26c6:b0:b71:df18:9fd6 with SMTP id a640c23a62f3a-b71df18a46fmr114924666b.50.1762191140354; Mon, 03 Nov 2025 09:32:20 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: dfgpostgres Date: Mon, 3 Nov 2025 12:32:09 -0500 X-Gm-Features: AWmQ_bmg0JH4J1CTsHN-2A9Vu8cRbiyYxdgLyzLHI2jBtQMxkwZBXQnPMJI8YAQ Message-ID: Subject: Re: PG Unpivot ? To: depesz@depesz.com Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000f27ede0642b4186b" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000f27ede0642b4186b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Wow ! That did it, even with the predicate I stuck on the end when I tried it. Thanks Depesz ! On Mon, Nov 3, 2025 at 12:22=E2=80=AFPM hubert depesz lubaczewski wrote: > On Mon, Nov 03, 2025 at 12:18:55PM -0500, dfgpostgres wrote: > > psql (13.2, server 15.3) on linux > > > > I think they call this "unpivot" in MSSQL ? > > > > How can I get an sql query to return one line per column with... an ID= , > > column name and value. the ctid for the id field is fine. > > > > Example: > > dvdb=3D# create table unpivot (intcol integer, floatcol float, strcol > > varchar); > > CREATE TABLE > > dvdb=3D# insert into unpivot (intcol,floatcol,strcol) values > > (1,1.1,'one'),(2,2.2,'two'),(3,3.3,'three'); > > INSERT 0 3 > > dvdb=3D# select * from unpivot; > > intcol | floatcol | strcol > > --------+----------+-------- > > 1 | 1.1 | one > > 2 | 2.2 | two > > 3 | 3.3 | three > > (3 rows) > > I want 9 records returned, each row with 3 cols, 1st col is the ctid, > > second is the column name, third is the val. > > I think it should work: > > select u.ctid, e.* from unpivot u, to_jsonb(u) j, jsonb_each_text(j) e; > > Best regards, > > depesz > > --000000000000f27ede0642b4186b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Wow !
That did it, even with=C2=A0the predicate I stuck= on the end when I tried it.

Thanks Depesz !

On= Mon, Nov 3, 2025 at 12:22=E2=80=AFPM hubert depesz lubaczewski <depesz@depesz.com> wrote:
On Mon, Nov 03, 2025 at 12:1= 8:55PM -0500, dfgpostgres wrote:
> psql (13.2, server 15.3) on linux
>
> I think they call this "unpivot" in MSSQL ?
>
> How can I get an sql query to return=C2=A0 one line per column with...= an ID,
> column name and value.=C2=A0 the ctid for the id field is fine.
>
> Example:
> dvdb=3D# create table unpivot (intcol integer, floatcol float, strcol<= br> > varchar);
> CREATE TABLE
> dvdb=3D# insert into unpivot (intcol,floatcol,strcol) values
> (1,1.1,'one'),(2,2.2,'two'),(3,3.3,'three'); > INSERT 0 3
> dvdb=3D# select * from unpivot;
>=C2=A0 intcol | floatcol | strcol
> --------+----------+--------
>=C2=A0 =C2=A0 =C2=A0 =C2=A01 |=C2=A0 =C2=A0 =C2=A0 1.1 | one
>=C2=A0 =C2=A0 =C2=A0 =C2=A02 |=C2=A0 =C2=A0 =C2=A0 2.2 | two
>=C2=A0 =C2=A0 =C2=A0 =C2=A03 |=C2=A0 =C2=A0 =C2=A0 3.3 | three
> (3 rows)
> I want 9 records returned, each row with 3 cols, 1st col is the ctid,<= br> > second is the column name, third is the val.

I think it should work:

select u.ctid, e.* from unpivot u, to_jsonb(u) j, jsonb_each_text(j) e;

Best regards,

depesz

--000000000000f27ede0642b4186b--