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 1fapx8-00066h-H1 for pgsql-general@arkaria.postgresql.org; Wed, 04 Jul 2018 22:08:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fapx6-0000RH-Vl for pgsql-general@arkaria.postgresql.org; Wed, 04 Jul 2018 22:08:44 +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 1fapx5-0000RA-UL for pgsql-general@lists.postgresql.org; Wed, 04 Jul 2018 22:08:44 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fapx2-0006ER-CA for pgsql-general@postgresql.org; Wed, 04 Jul 2018 22:08:43 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 244C621A29; Wed, 4 Jul 2018 18:08:39 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 04 Jul 2018 18:08:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=zdzWrW+44vMmwrcsD8EuK9A46Rc/M 7cWH4Dfc8lePdI=; b=tcYtjoStNyZsEJEC9ZsE2ymcVxTqVOkVNFbOKLt2TVbeJ kYxLYWBL67cE1IXwkv2OzjzLKKqci7jVqMYjtJX4yoHSQ01AYiiprvdXWbhulXqR d1ahRKw7SI1+dT1AO1Rbajh1Mm8M5/lU4RD7IVcsUeSowM7yFo85mnFm9ruONoXs +Q69T54wW+KcJ/XSG4t5LAOApcmpudf5Z8Yf5gwlshyRVEvA/f0qhCJ/6Mp3zlbG 51GuJ6JPZOQYTGQwVarCWgibiAPxcoCKRmBCyf5Nn5oJK0UBtQHeUKFt9gRgmuVm 4yYZ/HtCx9Tqehu7d/gV7FMXFR1Ld4ZB5uhH8/DsQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=zdzWrW +44vMmwrcsD8EuK9A46Rc/M7cWH4Dfc8lePdI=; b=otBg3YHFMdufbsS6ObJDx9 KgLl4ukTVcC+wmFE+Ob7klyNSwVJc0OrrWBlxu+Qfe8euGoeyou8fwbK/g36P2ll GnB5YKrR1ARwpgpBcDQVqENfytXl/3stSZbaBtRXvuX+jIzLnsfv52PB6uBuFYkn qGqNfpEPlli+QHwduk9QrjWJtrRK3OJwtS9x5j1JhRb3gKbRg/lFwFMqKFqxpyBQ wjS0ip440ElyDuxwAGOoTASiegG0DpTJFZ/f8htLVbbplRATeoJQpxwFWRHwi5f4 OKgrfW5Z8fqqij5HAKnUuPaPHhSHijZY53igy/QZql+8s3naonKX5eMWsFf3YZ3A == X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.134.181]) by mail.messagingengine.com (Postfix) with ESMTPA id 4E1771026E; Wed, 4 Jul 2018 18:08:38 -0400 (EDT) Subject: Re: Return select statement with sql case statement To: Ron , pgsql-general@postgresql.org References: <3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.com> <54d91883-6b0d-a8be-5aa4-d70bd5867c7c@gmail.com> From: Adrian Klaver Message-ID: <26b7bb4e-b9cf-1843-28e5-c81ef7b3f64f@aklaver.com> Date: Wed, 4 Jul 2018 15:08:37 -0700 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: <54d91883-6b0d-a8be-5aa4-d70bd5867c7c@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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:( > > -- Adrian Klaver adrian.klaver@aklaver.com