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 1qghSM-00GsWO-EQ for pgsql-sql@arkaria.postgresql.org; Thu, 14 Sep 2023 08:12:10 +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 1qghSK-006kEq-Qz for pgsql-sql@arkaria.postgresql.org; Thu, 14 Sep 2023 08:12:08 +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 1qghSK-006kEe-Ep for pgsql-sql@lists.postgresql.org; Thu, 14 Sep 2023 08:12:08 +0000 Received: from mail-ua1-x92d.google.com ([2607:f8b0:4864:20::92d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qghSC-004zw3-IV for pgsql-sql@postgresql.org; Thu, 14 Sep 2023 08:12:07 +0000 Received: by mail-ua1-x92d.google.com with SMTP id a1e0cc1a2514c-7a282340fdfso255399241.0 for ; Thu, 14 Sep 2023 01:12:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694679117; x=1695283917; darn=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=+bhTPqhhAsvADqdVUmMfdNumD/Ekpl9AdYQFZGC6kXc=; b=bwRnX4GuvaurkuNFiMEgNhsO94DRGAwfOyaIILldNRzR/FxUuIsi0RhvNSLZTjKyVG 6JxxZTg0kaExyixfrZ4zdZjbAdNQ7We7lVMNJJp245Vn8+xXh8OgRmMzjSvQp1yu8pIK 67N1lzxt6CBZ3/UTy1mJQEqq661C2zEWHZjrfmB4RCNairFCVz+B6oFlWgFL0AlXqsb5 K9IE+2ftpu6a0p6R25zGEb+Ulz33VsfWAP2ZIh9dFiFERvmGmanb1bQqL6//3/Mw4+br V/QJo9pnEjy7c6dOw0fizRQbKb8tM2y4Tss+eW9aiVSDq3P8hktmSp+u+LESwwWUB/4d ejGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694679117; x=1695283917; 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=+bhTPqhhAsvADqdVUmMfdNumD/Ekpl9AdYQFZGC6kXc=; b=mCSiV5dk4kjQ0JGpuBR/VTJIBFeqWNaBx7z/Tk9fuctFjDL3hFg8CZn5iP4NDeTryA MqIp+O9Ktp8strQ67VH4BmD3uQh50im3+Mz2JTuR58vRaW+DRKgBA9mLBdCPfLlnzxJS xV1GbaTY528vS4SX13pli/Rc71WCfGDpSMgP+s8TGTIe6DKXj+m51hnBXc6wNSKhUzyb TYm364Sy4QnO+ZR1PaI6dSsvt6EjM/bsbgFG9k2ifSG77K2VikGxTILwMFF+exAL94eI ktNGGHTl3lndytW0ii55vBsev4Cw5jgpsNxEsJQiVmDDql51eo970KFzNd2zAEYqigPY inPw== X-Gm-Message-State: AOJu0YxDR6dM+9v7VvgUQgo5ohTo4dP0/YUZC6hPIhnv+OL9rwvnZ6In iUcQNPzO0NBkyL8kuSrzWah/IwH7qRDozo+RKRU= X-Google-Smtp-Source: AGHT+IHnr7SzvCI6N8YomS+QsYpZOvJz9dwL+gl1FvGCLLPkoSZqL71RYJvYf3TgySAc4l1Q5q56Puqs/hhNu01YREc= X-Received: by 2002:a1f:ed47:0:b0:48d:b7c:56c8 with SMTP id l68-20020a1fed47000000b0048d0b7c56c8mr4497236vkh.0.1694679115859; Thu, 14 Sep 2023 01:11:55 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Mehmet Sabri KUNT Date: Thu, 14 Sep 2023 11:11:30 +0300 Message-ID: Subject: Re: Query with conditional statement To: JORGE MALDONADO Cc: pgsql-sql@postgresql.org Content-Type: multipart/alternative; boundary="000000000000b58b7d06054d3a71" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000b58b7d06054d3a71 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Jorge, You can not use the case statement like in your code. If you want to get a different field's value by your condition you can use the case statement like the following examples. select id,CASE WHEN condition1 THEN fldx1 END AS a1,CASE WHEN condition1 THEN fldx2 END AS a2,CASE WHEN condition1 THEN fldx3 END AS a3,CASE WHEN condition2 THEN fldy1 END AS b1,CASE WHEN condition2 THEN fldy2 END AS b2,CASE WHEN condition2 THEN fldy3 END AS b3FROM your_table If you use like above, you have lots of null values. If you don't have to use column names like a1, a2,a3, b1,b2, or b3 like in your code, I suggest using like the following selectid,CASE WHEN condition1 THEN fldx1 WHEN condition2 THEN fldy1 ELSE aaa1END AS a1,CASE WHEN condition1 THEN fldx2 WHEN condition2 THEN fldy2 ELSE aaa2END AS a2,CASE WHEN condition1 THEN fldx3 WHEN condition2 THEN fldy3 ELSE aaa3END AS a3FROM your_table Shane Borden , 14 Eyl 2023 Per, 01:43 tarihinde =C5=9F= unu yazd=C4=B1: > You will need to do a CASE statement for each column. I=E2=80=99m not aw= are of > being able to return multiple columns from one case. > > Shane Borden > sborden76@gmail.com > Sent from my iPhone > > On Sep 13, 2023, at 4:23 PM, Tchouante, Merlin > wrote: > > =EF=BB=BF > > Yes, it can. > > > > *Thanks,* > > * -- Merlin* > > > > > > *Merlin D. Tchouante,* Sr. IT Enterprise Application Developer > *Center for Information Technology Services (CITS)* > *601 West Lombard Street* > *Baltimore, Maryland 21201-1512* > *mtchouan@umaryland.edu* > 410-706-4489 * 410-706-1500 fax > > > > *Please send Blackboard questions to the CITS support email address:* > DL-CITSBbSupport@umaryland.edu > > *Please send Mediasite questions to the CITS support email address:* > DL-CITSMediasiteSupport@umaryland.edu > > > > > > > > *From:* JORGE MALDONADO > *Sent:* Wednesday, September 13, 2023 1:52 PM > *To:* pgsql-sql@postgresql.org > *Subject:* Query with conditional statement > > > > You don't often get email from jorgemal1960@gmail.com. Learn why this is > important > > *CAUTION: *This message originated from a non-UMB email system. Hover > over any links before clicking and use caution opening attachments. > > 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 > > --000000000000b58b7d06054d3a71 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Jorge,
You can not use the case statement like in y= our code.

If you want to get a different field's valu= e by your condition you can use the case statement like the following examp= les.


select 
id,
CASE WHEN condition1 THEN fldx1 END AS a1,
CASE WHEN condition1 THEN fldx2 END AS a2,
CASE WHEN condition1 THEN fldx3 END AS a3,
CASE WHEN condition2 THEN fldy1 END AS b1,
CASE WHEN condition2 THEN fldy2 END AS b2,
CASE WHEN condition2 THEN fldy3 END AS b3
FROM your_table

If you use like above, you have lots of= null values.=C2=A0=C2=A0

If you don't have to= use column names like a1, a2,a3, b1,b2, or b3 like in your code, I suggest= using like the following

select
id,
CASE 
   =
     WHEN condi=
tion1 THEN fldx=
1 
   =
     WHEN condi=
tion2 THEN fldy=
1 
   =
     ELSE aaa1<=
/span>
END AS a1,
CASE 
   =
     WHEN condi=
tion1 THEN fldx=
2 
   =
     WHEN condi=
tion2 THEN fldy=
2 
   =
     ELSE aaa2<=
/span>
END AS a2,
CASE 
   =
     WHEN condi=
tion1 THEN fldx=
3 
   =
     WHEN condi=
tion2 THEN fldy=
3
   =
     ELSE aaa3<=
/span>
END AS a3
FROM your_table

Shane Borden <sborden76@gmail.com>, 14 Eyl 2023 Per, 01:43 tarihinde =C5=9Funu = yazd=C4=B1:
You will need to do a CASE statement for each column.=C2=A0 I= =E2=80=99m not aware of being able to return multiple columns from one case= .=C2=A0

Shane Borden
sborden76@gmail.com
Sent from my= iPhone

On = Sep 13, 2023, at 4:23 PM, Tchouante, Merlin <mtchouan@umaryland.edu> wrote:
<= br>
=EF=BB=BF

Yes, it can.=

=C2=A0

Thanks,=

=C2=A0 -- Merlin=

=C2=A0

=C2=A0

Merlin D. Tchouante,=C2=A0<= /span>Sr. IT Enterprise Application Developer
Center for Information Techno= logy Services (CITS)
601 West Lombard Street<= /i>
Baltimore, Maryland 21201-1512<= /span>
mtchouan@umaryland.edu=
=C2=A0
410-706-4489 * 410-706-1500 fax=

=C2=A0

Please send Blackboard questions to the CITS support email add= ress:=C2=A0=C2=A0DL-CITSBbSupport@um= aryland.edu

Please send Mediasite questions to the CITS support email addr= ess:=C2=A0=C2=A0DL-CITSMediasiteSupport@umaryland.edu

=C2=A0

<image001.png>

=C2=A0

From: JORGE MALDONADO <jorgemal1960@gmail.com> Sent: Wednesday, September 13, 2023 1:52 PM
To: pg= sql-sql@postgresql.org
Subject: Query with conditional statement

=C2=A0

CAUTION: This message originated from a non-UMB emai= l system. Hover over any links before clicking and use caution opening atta= chments.

Hi,

=C2=A0

Can a conditional CASE statement be part of the SELE= CT portion=C2=A0of a query? For=C2=A0example:

=C2=A0

SELECT

fld1, fld2, fld3,

CASE=C2=A0

=C2=A0 =C2=A0 WHEN condition1 THEN fldx1 AS a1, fldx= 2 AS a2, fldx3 AS a3

=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 ......

=C2=A0

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<= u>

=C2=A0

I need to return more than 1 field on each WHEN and = also assign an ALIAS.

I very much appreciate your feedback.<= /p>

=C2=A0

Regards,

Jorge Maldonado

--000000000000b58b7d06054d3a71--