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 1vFyD3-004kh4-Kl for pgsql-general@arkaria.postgresql.org; Mon, 03 Nov 2025 17:19:12 +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 1vFyD2-006aRX-Ik for pgsql-general@arkaria.postgresql.org; Mon, 03 Nov 2025 17:19:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vFyD2-006aRP-80 for pgsql-general@lists.postgresql.org; Mon, 03 Nov 2025 17:19:11 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vFyCz-005Fdm-1S for pgsql-general@lists.postgresql.org; Mon, 03 Nov 2025 17:19:10 +0000 Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-640bc4ced7bso570022a12.3 for ; Mon, 03 Nov 2025 09:19:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762190347; x=1762795147; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=TBIm0i0/oiUT7aqvxbOEhlt6lwEaYmLDImbpcoXhBE0=; b=imwx6q2cOKCC05ZdOvuWigkUtlPo+UOdFmJzpcrTP4wFz3rvba6GGwKVIY/tEpxUTv lpN3Cz93aJR0/8oB3LtGXi5+01VU2oPbcCTaIwyr667aKYuKPbRMf0J1WnF19tRhSzxM bTnc6If/s5lk6L4soLYWcxHr98Uff9XQaC38ZWbVq3T4VKibmkDvLRO/58xs1TUM1zeY yrwRHMXDbtm4dOc+zNtskGlzRo1nML+br030y6ebb4OD3GMGZdRMAyH23g80Jfy3XUUy BAsvA4ptQauXGTjtAxZctFycagk8fZ89eMTi47z0UwbQMyBSKBxDlHye6DnXcc2SIghD 3GDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762190347; x=1762795147; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=TBIm0i0/oiUT7aqvxbOEhlt6lwEaYmLDImbpcoXhBE0=; b=oEGshuqpk8UMOYhmvlYRchSJ1mlty5GxkC6wXymkRlVH6pQr1lUK/Imf/4rFIlLZP0 Vp2Qno18/vZ391Zn7K6Bddf01fVDVUMEPFbzQpz68ZsJ4t+CO81ewgly9eCdOLR6QNs4 m1xeeYWUgswuNhQpMovuuo5KWBntM8ZkwGYFSd47RtJD80/sQ6SE0o9iqFG/ZksEQqJg sp70fCSaZSpGC9FXGz4SW4eYkcgAhYyYPm4Jyy3TLFfzZISiNUoYAIoQpJDcOso+dQm5 O+f4lQb225jaC8ZFXCKl1JUeDIZLtpevwHGWGG59utn0K5RjDPJRCfg2CYFtzjdFJtCh hQxQ== X-Gm-Message-State: AOJu0YxA44L4fwMFK9TkHbqQSQj1vhWsx/EwLws0KKdpxmJiXIlgFUlz RFmFG4Hj41jAIVRZupFfCvaDvYVlQgo5ZZbnalDiiKUt7hnnT+yJT9Bd7uf877XxbcbjBV6SfKp kp08DGc5aIyVyYTJB+3MIRCyKrYqhvv+4BZWc X-Gm-Gg: ASbGncs8rp7feleWg0WsUZgeQ5P97YnDcx2LRRqyBbjKswwBVwr6FATDCEt+OijFTsA qhFL/utJENzx9T+IhoGGr9skc2A7CW9SYzaqT4UrKBxEHBCH8KZXfAXhXXR9xe053pTXAt3BRVX Lc7hu+Uqlvi3Re9hZdFn/CvOGJgVvnT+gDtz1vLdaeLA8H13obpx4YJzgGUkO0cnUBpIlG/4pbw 201MaR5NJit6xWWwXSDDuvPjwf4YCtwlVizwDQMAH0xTjBT1cAowk6zrNQX9w== X-Google-Smtp-Source: AGHT+IEA/NgyTZzW13DCTclfE2t4Nww8AooeytACgy0O7BK9t+5qxST2pNi8WGbUuvzzyYKGXl3EkUsZc0xar3Uue0I= X-Received: by 2002:a17:907:3d0e:b0:b3e:f89e:9861 with SMTP id a640c23a62f3a-b70701ac51amr1459408266b.28.1762190346671; Mon, 03 Nov 2025 09:19:06 -0800 (PST) MIME-Version: 1.0 From: dfgpostgres Date: Mon, 3 Nov 2025 12:18:55 -0500 X-Gm-Features: AWmQ_blw60Er_HZv_WfxjlEkicPlX_CSztuYZG1BgCR6Bms3qHjj_VAmi5RkGJw Message-ID: Subject: PG Unpivot ? To: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000a3d9f30642b3e9f0" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000a3d9f30642b3e9f0 Content-Type: text/plain; charset="UTF-8" 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=# create table unpivot (intcol integer, floatcol float, strcol varchar); CREATE TABLE dvdb=# insert into unpivot (intcol,floatcol,strcol) values (1,1.1,'one'),(2,2.2,'two'),(3,3.3,'three'); INSERT 0 3 dvdb=# 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. Thanks in Advance ! --000000000000a3d9f30642b3e9f0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
psql (13.2, server 15.3) on linux

I thi= nk they call this "unpivot" in MSSQL ?

H= ow 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.=C2=A0=C2= =A0

Example:
dvdb=3D# create table unpiv= ot (intcol integer, floatcol float, strcol varchar);
CREATE TABLE
dvd= b=3D# insert into unpivot (intcol,floatcol,strcol) values (1,1.1,'one&#= 39;),(2,2.2,'two'),(3,3.3,'three');
INSERT 0 3
dvdb= =3D# select * from unpivot;
=C2=A0intcol | floatcol | strcol
-------= -+----------+--------
=C2=A0 =C2=A0 =C2=A0 1 | =C2=A0 =C2=A0 =C2=A01.1 |= one
=C2=A0 =C2=A0 =C2=A0 2 | =C2=A0 =C2=A0 =C2=A02.2 | two
=C2=A0 = =C2=A0 =C2=A0 3 | =C2=A0 =C2=A0 =C2=A03.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.

Thanks in Advance !

--000000000000a3d9f30642b3e9f0--