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 1qgU3L-00FyzT-AX for pgsql-sql@arkaria.postgresql.org; Wed, 13 Sep 2023 17:53:27 +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 1qgU3J-001Fbb-RU for pgsql-sql@arkaria.postgresql.org; Wed, 13 Sep 2023 17:53:25 +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 1qgU3J-001FbP-Gh for pgsql-sql@lists.postgresql.org; Wed, 13 Sep 2023 17:53:25 +0000 Received: from mail-oa1-x2a.google.com ([2001:4860:4864:20::2a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qgU3G-004sme-3y for pgsql-sql@postgresql.org; Wed, 13 Sep 2023 17:53:24 +0000 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-1ba5cda3530so47360fac.3 for ; Wed, 13 Sep 2023 10:53:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694627599; x=1695232399; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=tFp4Yeao2hT8nRXLZgho7PxI+R2qE7OYG8hA0H+Jiow=; b=Iy3ddFs3xzZH6L5kJzSe9vVtuXNJSLuAelD7dVg8wRtcs9UM6kxjaxz8wBQiktV1q+ nXSl6VjDKh5egh12VxbhmY1gkyp+upxiJaPeeZTsrFWEKTDQu20KRY8wXSuRzRUAbL4W uDSMyUUGRUHq9FMz/hgJPkus/Fw9qKbN175aCarWKbiPkGFYjvlzfx17iIiFfw3GJb2+ zR0fgK5eOxchJnF2FwGNXxCA5RZY+IcXgFJkHqG34k6ZKwb87DCY+Fr8ZgSfbiwBzQ/c rxlpxPobKYt9yUoqe4e/WreTpiJi1jOVfmNp4BcSHaJo69jcuCN0TfMbuH5a6kZqj+/C WKqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694627599; x=1695232399; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=tFp4Yeao2hT8nRXLZgho7PxI+R2qE7OYG8hA0H+Jiow=; b=rrWLlFgO7TOyiPqkX1OuGR/Zqc3/Lb7ND9dCJYuiFjYe1H0fvp5ro8x0aq+hu00+Cz Eie4dFsZSsuXAru1RAfbby9T8I54ryMxvJ9wLG5TaFlPVD1RJ0Fd4VTE0a8XNLw3xTGE AkiDwno1N+gXGBoraYN+PPkii2C29F0EoCYhogYX5/x79MscP6GebWWV8p7Z1JiKavPw XE47lDfUPRPwQgkFB3GN7SrPd0Y/Iw7RoQSkvrJXrQlHRP3SdbI6jEmY399CrjqKSUOE ju2oKlLWCSy7XBmYGscHXK81mHR3S2WUtpj39IepXRIA9ZA3eipMUJL3eyrlNO6sykZ0 cdog== X-Gm-Message-State: AOJu0YxfNa3Hzq154p6oe1KO6I+F7PwJ11QJkuQIOev1LWgO3eUEPZnI xTLK3tTLXT/aiuSnt48rNordSMjXP5YGy2nQYpNpPyAw9ZE= X-Google-Smtp-Source: AGHT+IE05jnfiFkFdoHNu6KOtq7AAxdIdavSVnbERanYPPaxIvRaB0xfbu0cpPzZqQbNqnoFXLsSatRloS9MZABkTbY= X-Received: by 2002:a05:6870:d61e:b0:1b0:408a:1d14 with SMTP id a30-20020a056870d61e00b001b0408a1d14mr4311970oaq.44.1694627599086; Wed, 13 Sep 2023 10:53:19 -0700 (PDT) MIME-Version: 1.0 From: JORGE MALDONADO Date: Wed, 13 Sep 2023 12:52:18 -0500 Message-ID: Subject: Query with conditional statement To: pgsql-sql@postgresql.org Content-Type: multipart/alternative; boundary="00000000000011f9200605413c78" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000011f9200605413c78 Content-Type: text/plain; charset="UTF-8" Hi, Can a conditional CASE statement be part of the SELECT portion of a query? For example: SELECT fld1, fld2, fld3, CASE WHEN condition1 THEN fldx1 AS a1, fldx2 AS a2, fldx3 AS a3 WHEN condition2 THEN fldy1 AS b1, fldy2 AS b2, fldy3 AS b3 ELSE ..... END, fld6, fld7 FROM ...... WHERE ...... I ran a test and see the following: * Each WHEN only accepts 1 result and not 3 as shown in the example * The AS for the alias is not supported I need to return more than 1 field on each WHEN and also assign an ALIAS. I very much appreciate your feedback. Regards, Jorge Maldonado --00000000000011f9200605413c78 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

Can a conditional CASE statement be= part of the SELECT portion=C2=A0of a query? For=C2=A0example:
SELECT
fld1, fld2, fld3,
CASE=C2=A0
<= div>=C2=A0 =C2=A0 WHEN condition1 THEN fldx1 AS a1, fldx2 AS a2, fldx3 AS a= 3
=C2=A0 =C2=A0 WHEN condition2 THEN fldy1=C2=A0AS b1, fldy2=C2= =A0AS b2, fldy3=C2=A0AS b3
=C2=A0 =C2=A0 ELSE .....
END= ,
fld6, fld7
FROM ......
WHERE ......

I ran a test and see the following:
* Each WHEN= only accepts 1 result and not 3 as shown in the example
* The AS= for the alias=C2=A0is not supported

I need to ret= urn more than 1 field on each WHEN and also assign an ALIAS.
I ve= ry much appreciate your feedback.

Regards,
=
Jorge Maldonado
--00000000000011f9200605413c78--