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 1faiuB-0006EY-1k for pgsql-general@arkaria.postgresql.org; Wed, 04 Jul 2018 14:37:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1faiu8-00042s-QW for pgsql-general@arkaria.postgresql.org; Wed, 04 Jul 2018 14:37:12 +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 1faiu8-00042l-GF for pgsql-general@lists.postgresql.org; Wed, 04 Jul 2018 14:37:12 +0000 Received: from mail-oi0-x244.google.com ([2607:f8b0:4003:c06::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1faiu4-0004UV-Up for pgsql-general@lists.postgresql.org; Wed, 04 Jul 2018 14:37:12 +0000 Received: by mail-oi0-x244.google.com with SMTP id n84-v6so11110433oib.9 for ; Wed, 04 Jul 2018 07:37:08 -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=mzdsPl1V5fScvfa4uGlRsRfeFWC1fKl6VTii/oMLxUU=; b=nQSCBZaRU5/pgtrttrCFeOmyrcEOdEMi2DMHcmgR+Kz5ai53zb8u5GO2dKfFFUSjHS UvnXLh+9IBsKvOlwrMhSVrqGXT+e8yxXaJb4A3flk1fnDKXh9CXzsSfV0XljOiJjLyez 8ZShbjjMbcwFNy6UEh9blwurIjtBqNK2nqZVVJErFwtjQuOtrl69r9kfGb4TxhxLiz/6 vmquDyEB8niIsYtH8UGYKFIH+davu1f/eC3G/W57XTdtA9dbzkUKeb8UxCJt4pP2euLK weSzOc/c/o6Pcy3OVH+ouuIKZy0M11P5l+FwbyHYPI4fyTPMX2lRNQ3akUjsndqoVLTt i4Rg== 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=mzdsPl1V5fScvfa4uGlRsRfeFWC1fKl6VTii/oMLxUU=; b=TmaM9XaYq3vPu6tVuJPQTRx5wTCPBzgHNB9drHJCyVXC06k1aW3F0z2NVJQ6ZRyUgl E9FJNB/r0LkwhmdensP5Ngu/6+VqNacJ2E2cB39Nr1ChZesIu1Kmh/Uxw5EbEYlv6cKf 4s+a+vbw2rrOiurNmm9nQI3nB7HH3BlIhuEtt/NceZN2uEoz/3zFvBmRuC9aDTfM4YmQ XZLMw99MZBqq5wIg+PqNlB0Ub40wB3ytK34W0FX4IHEDJoJ0r2fYXDP7PyCj1dreFlo0 abWpUPCmFvxrDw8KaBym3mPri9RSt/gL8SBIxyoiIjHX2GV99vsR9EfQaXoC6+aSO4K+ wsjQ== X-Gm-Message-State: APt69E04KRjBIOihiXgXjBmiqwN3qvU/A91UwIFTXmO7Xa3uGHVqFdft v7n54PqIqHA9LMqCizxTQs6KRg== X-Google-Smtp-Source: AAOMgpfM+WCmoCdEVEsDntfSz+Ubu7Nk92dlZXGrj/49q83VFkjo6EeNxI/jJ0lkeKhr0tZYQK7MSw== X-Received: by 2002:aca:d0a:: with SMTP id 10-v6mr2628341oin.277.1530715026303; Wed, 04 Jul 2018 07:37:06 -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 y3-v6sm1818210oie.23.2018.07.04.07.37.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 07:37:05 -0700 (PDT) Subject: Re: Return select statement with sql case statement To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.com> Date: Wed, 4 Jul 2018 09:37:04 -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 07:48 AM, hmidi slim wrote: > Hi, > I need to use conditional expression in my query, So I want to make a > query like this: > select numberOfPremiumDays >             case  when numberOfPremiumDays = date_part('day', > ('2018-11-05'::timestamp) - ('2018-11-01'::timestamp)) then >                 select product_id, >                 price >                 from product >                 where occupation_type_id = 1 >                 group by product_id, occupation_type_id > > However, in the documentation I found that the return of case was a value, > not like in my case I want to return a select statement. > How can I use a conditional expression in a sql query? > Best Regards. The CASE clause is used to return one of many choices.  Based on this example, you need to do this: select numberOfPremiumDays,        product_id,        price        from product where occupation_type_id = 1   and  numberOfPremiumDays = date_part('day', ('2018-11-05'::timestamp) - ('2018-11-01'::timestamp)) group by product_id, occupation_type_id -- Angular momentum makes the world go 'round.