Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fapsc-0005q2-JX for pgsql-general@arkaria.postgresql.org; Wed, 04 Jul 2018 22:04:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fapsY-00062R-Me for pgsql-general@arkaria.postgresql.org; Wed, 04 Jul 2018 22:04:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fapsY-00062I-Bc for pgsql-general@lists.postgresql.org; Wed, 04 Jul 2018 22:04:02 +0000 Received: from mail-oi0-x243.google.com ([2607:f8b0:4003:c06::243]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fapsV-00066U-LC for pgsql-general@postgresql.org; Wed, 04 Jul 2018 22:04:01 +0000 Received: by mail-oi0-x243.google.com with SMTP id m2-v6so13086440oim.12 for ; Wed, 04 Jul 2018 15:03:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=Z0y4lFnAArP1VQcv9pxlPhiwngZrypMfTL2CU7+jbJw=; b=tnSD81IG9qgpMeQrTYfy9EccQeztXQoaT0hbspJy8b4RbGFc4jbPITHCm5licXjqzl WTOnpJwlIMJbXAkTIZYbXEnn4S7MrdFJib+PSFuHqwsKPzmEGigksQvZXg8QFWcAs/UH EADmsymKn+XgHpc9UTISeS8f9qOBRPGF9nnnOBGHBzNg+ynle6UAmvXZfkp5N0kqFj+D lyS+RuBDi7Cyh0XdPy1ZP3BXVVu0F/8xsm4T6y13U6/Bi+mjAd5kkaMfs/DKDIDCyBPe vGzEUJPbVzjWDVv74vtJMd6R5/F/OSwNhZDTDdfqN2DqD0TSHCShtivMPEdV2p7LJAvU jqSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=Z0y4lFnAArP1VQcv9pxlPhiwngZrypMfTL2CU7+jbJw=; b=LvpSb6WecrmpkmQw+GBw5OunGvtbY8ePlst2xyBmTSMv0cKB4yEyoxzqS6NuTnVJDX x2r2eVmny6n04gthDfoEjZwSlvsub0pbPAccRaA40xqqNA5F4D4oseu3LFw3ykJpadCZ up6r4+Ap9gFpOjjasjFs5MJQOIbLIs+oxPKG1XFoHOaJNP0Yjxq5JG4Yg8O2jpHSY7/A Va1kNlbBIEnuO/xBr5tUuFl/wWHJFX9StOVEMJz9Oy7r5b3kJmWewa0NKA9es7X/LOHF VzIMDmpGQMAWZz+PglGJy0ldWPsBqsO0Oeaop/K7m0G2cBvo2TDd6ry6Bf5u1+cuxFQ0 U1Sg== X-Gm-Message-State: APt69E20a3rfCNhCVpBluCK88k/483DF55cHktRtSBzJvNvNTjcU54jj FVm7zP/5nWip4om7AZ/Y5asOfg== X-Google-Smtp-Source: AAOMgpdyLvIGzcIRfOKdLQhZAfqi8qMhI+9zg51wShsiovqdq2H0tEOx1TCS8WBDGV1qNZpP8cnXDA== X-Received: by 2002:aca:804:: with SMTP id 4-v6mr3794035oii.333.1530741837295; Wed, 04 Jul 2018 15:03:57 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id 4-v6sm2458304oif.34.2018.07.04.15.03.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 15:03:56 -0700 (PDT) Subject: Re: Return select statement with sql case statement To: pgsql-general@postgresql.org References: <3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.com> From: Ron Message-ID: <54d91883-6b0d-a8be-5aa4-d70bd5867c7c@gmail.com> Date: Wed, 4 Jul 2018 17:03:55 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 07/04/2018 10:32 AM, hmidi slim wrote: > Actually, I need the use of case because based on the numberOfPremiumDays > there are different type of treatment: > select numberOfPremiumDays >             case  when numberOfPremiumDays = date_part('day', > ('2018-11-05'::timestamp) - ('2018-11-01'::timestamp)) then >                 select product_id, >                 premium_price, >                 period_price >                 from product >                 where occupation_type_id = 1 >                 group by product_id, occupation_type_id >            else >                 select product_id, >                 classic_price, >                 period_price >                 from product1 >                 where occupation_type_id = 1 >                 group by product_id, occupation_type_id > Then try: select product_id, case when numberOfPremiumDays = date_part('day', ('2018-11-05'::timestamp) - ('2018-11-01'::timestamp)) then            premium_price        else            period_price        end as the_price from product where occupation_type_id = 1 order by product_id, occupation_type_id -- Angular momentum makes the world go 'round.