Thread

  1. Query with conditional statement

    JORGE MALDONADO <jorgemal1960@gmail.com> — 2023-09-13T17:52:18Z

    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
    
  2. RE: Query with conditional statement

    Tchouante, Merlin <mtchouan@umaryland.edu> — 2023-09-13T17:57:54Z

    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<mailto:mtchouan@umaryland.edu>
    410-706-4489 * 410-706-1500 fax
    
    Please send Blackboard questions to the CITS support email address:  DL-CITSBbSupport@umaryland.edu<mailto:dl-citsbbsupport@umaryland.edu>
    Please send Mediasite questions to the CITS support email address:  DL-CITSMediasiteSupport@umaryland.edu<mailto:DL-CITSMediasiteSupport@umaryland.edu>
    
    [cid:image001.png@01D9E64A.49E98770]
    
    From: JORGE MALDONADO <jorgemal1960@gmail.com>
    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<mailto:jorgemal1960@gmail.com>. Learn why this is important<https://aka.ms/LearnAboutSenderIdentification>
    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
    
  3. Re: Query with conditional statement

    David G. Johnston <david.g.johnston@gmail.com> — 2023-09-13T18:07:05Z

    On Wed, Sep 13, 2023 at 10:53 AM JORGE MALDONADO <jorgemal1960@gmail.com>
    wrote:
    
    > I need to return more than 1 field on each WHEN and also assign an ALIAS.
    >
    >
    You can only produce a single value of a specific data type from the output
    of a case expression.   To do what you desire, create a custom composite
    type and return that.  You get to name the fields when you define the
    composite type.
    
    David J.
    
  4. Re: Query with conditional statement

    Shane Borden <sborden76@gmail.com> — 2023-09-13T20:39:49Z

    You will need to do a CASE statement for each column.  I’m not aware 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 <mtchouan@umaryland.edu> wrote:
    
        
    
      
    
    
      
        
        
        
        
          
    
            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
            
            
    
               
            
            
    
            
              <image001.png>
            
          
          
    
             
          
          
            
    
              From: JORGE MALDONADO <jorgemal1960@gmail.com>
    
              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
                
              
            
          
        
      
    
    
  5. Re: Query with conditional statement

    Mehmet Sabri KUNT <msabrikunt@gmail.com> — 2023-09-14T08:11:30Z

    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 <sborden76@gmail.com>, 14 Eyl 2023 Per, 01:43 tarihinde şunu
    yazdı:
    
    > You will need to do a CASE statement for each column.  I’m not aware 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 <mtchouan@umaryland.edu>
    > wrote:
    >
    > 
    >
    > 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* <mtchouan@umaryland.edu>
    > 410-706-4489 * 410-706-1500 fax
    >
    >
    >
    > *Please send Blackboard questions to the CITS support email address:*
    > DL-CITSBbSupport@umaryland.edu <dl-citsbbsupport@umaryland.edu>
    >
    > *Please send Mediasite questions to the CITS support email address:*
    > DL-CITSMediasiteSupport@umaryland.edu
    >
    >
    >
    > <image001.png>
    >
    >
    >
    > *From:* JORGE MALDONADO <jorgemal1960@gmail.com>
    > *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 <https://aka.ms/LearnAboutSenderIdentification>
    >
    > *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
    >
    >