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 1faq6j-0006bQ-HY for pgsql-general@arkaria.postgresql.org; Wed, 04 Jul 2018 22:18:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1faq6h-0006GN-Ju for pgsql-general@arkaria.postgresql.org; Wed, 04 Jul 2018 22:18:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1faq6h-0006GG-6T for pgsql-general@lists.postgresql.org; Wed, 04 Jul 2018 22:18:39 +0000 Received: from mail-oi0-x241.google.com ([2607:f8b0:4003:c06::241]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1faq6d-0002N2-Gg for pgsql-general@postgresql.org; Wed, 04 Jul 2018 22:18:37 +0000 Received: by mail-oi0-x241.google.com with SMTP id r16-v6so13168415oie.3 for ; Wed, 04 Jul 2018 15:18:35 -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=GiC+Vww7JPPV6oylGU3YSM2Wktks3S3ydLHcAs5aVkk=; b=Bhm7BmL93uFBNvmoBjnmyCDQnGRyFG/aFwa2f/ViHpHNtMCTnRKNb8sUBro8ZkTh9L /WzF+YckAsSBpytALCm2nMQ4GxAOvt5nkdi0UA6fHD6VtWe13CqavrWPqwo8ia5Orcyc tpl07Gm2O0VZ1gonp/VYG+NRyFZ551OKWLHGbE05jUERrvuLTAX5VIj5q310nSZ2jFmO 0630k7XbkuhUEgOaYqs31X+q9CwbJnxrE6+jVUo/xI2Uk9tcsU1eJh4SrdT8ruPM72+B B9WKVV/nEYt7lKeIJdOx8udHg2ZE7q26j+mJjIlVxs8n3iVvrp8H+oORAZQF7+CNUynK gDZw== 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=GiC+Vww7JPPV6oylGU3YSM2Wktks3S3ydLHcAs5aVkk=; b=ciiRScvvKW3iPI/fs+RqPeCMBA/ZCh/tPjaUNUZ3Qkf3RBN7NVIjwsqQH9ZJJlEZOz UTpwYdOhjoV8OFqlK2khKcpPpfrEbXsniM1r8T1htGLVFyEoAZ2dyb6riM6WBs1O2Pll XVYaMTeRLFHpqqvhJwxqSrxEvW6g+B9WxleS94RM0biAjiNpzYWbj9x3N8JUbc+T6Soh ZOhXl4KeiRsnhvYkXHVFCFb+og1nwNBZU+PxmluBjlo7Sc60EsmJNY1qeDpf7PbrP5qK JoVsRzt5w3bvyHPaKp8EpDJVRwuRk8H4vAsunfASEtwcLQJvHzkADOiQbLqKwHGwq2Cu V63Q== X-Gm-Message-State: APt69E2jrA39vGYfA4twhH9+WwSvN8rx0OIJVCeeFiMYeUktyJYxvuit o75KEL/aBJ5kH/yHGtHbm97NlQ== X-Google-Smtp-Source: AAOMgpcJau3P/uDqdext5Rut8KZO+ZumEcB0/f92o4Pe7D25qDMpwRymZmPOEZOM5kYKlV7z2g3ybQ== X-Received: by 2002:aca:3d4:: with SMTP id 203-v6mr3744415oid.321.1530742714670; Wed, 04 Jul 2018 15:18:34 -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 m3-v6sm2071298oif.26.2018.07.04.15.18.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 15:18:33 -0700 (PDT) Subject: Re: Return select statement with sql case statement To: Adrian Klaver , pgsql-general@postgresql.org References: <3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.com> <54d91883-6b0d-a8be-5aa4-d70bd5867c7c@gmail.com> <26b7bb4e-b9cf-1843-28e5-c81ef7b3f64f@aklaver.com> From: Ron Message-ID: Date: Wed, 4 Jul 2018 17:18:33 -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: <26b7bb4e-b9cf-1843-28e5-c81ef7b3f64f@aklaver.com> 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 05:08 PM, Adrian Klaver wrote: > On 07/04/2018 03:03 PM, Ron wrote: >> 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 > > The issue with the above is that table changes from product to product1 in > the OP's desired behavior so the price switch alone will not work:( Ah, didn't notice that.  Then... dynamic sql constructed by the programming language executing the query? -- Angular momentum makes the world go 'round.