Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kla1S-0004AG-4u for psycopg@arkaria.postgresql.org; Sat, 05 Dec 2020 16:02:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kla1R-0001SS-2q for psycopg@arkaria.postgresql.org; Sat, 05 Dec 2020 16:02:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kla1P-0001SL-IG for psycopg@lists.postgresql.org; Sat, 05 Dec 2020 16:02:56 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kla1L-0001n6-GP for psycopg@postgresql.org; Sat, 05 Dec 2020 16:02:54 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 6B292632; Sat, 5 Dec 2020 11:02:49 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sat, 05 Dec 2020 11:02:49 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=d NE2l4OOouuvhhmdc1nXfl9wN62m6vq+m7yZGQ899v4=; b=J9VTlW++Tm6cr4xPt ZmC9rkaybfTTVBBqI28EiWt671SrJbexKX7CLf/xJDiVmonwFOahxeMQsdiXke+I Ej+z+QZI2lsUPQpfOuryjVYTOs+ei4ofU5vUg8TwfsmiWkD5tSKAetMpQaq/vlv+ xFk6SJGLOMCn+RkC+orNratWaaZaWO+zX2BgukPas0pd5OW5wtfvsdHTyxx4iNDk FFqjpah7yc2OLITX3tM7MjwTiE6KIeUTOQlzRpFuEBiMm6LYg9Sph90lCg+gfGF8 2Q8jJo3oHQJhMN/DNlDlbnpmer8lO9tOvUI3EjixqLeCQP1HvnVChX5sDnQlkNeH j3GjQ== 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-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=dNE2l4OOouuvhhmdc1nXfl9wN62m6vq+m7yZGQ899 v4=; b=NRBeybtmuDJL3tHXoswYewiwJ05yZfuFVkvxK6tjmcNrwXaIpzXtohnYZ zgWDY9K0Tuf/M+9bx4xkUQX0mC0TRFL9fsT8vWX7u+KyUBj7z+Iuh7SeVbsVrRJ4 34LfRQH/3s5+CpMp1fjYRSu5+qEi0cvlDiXYNrBDB8maB1U30KNL1UpgfH3RC6pz M59qD8MQgbYs7ulSxg8b7KxEyVPtdKpXhgktLRfAZbFu98pD+BqIAqKFiRiCIWP5 Gw5KBA3SFRaa3ZbR5qv/I7LVHMa4B6s932GjQzjPcrtQIxiZQ/KbcFuA8IXg/ZjX +MNAva65xLLA+7+ihnQUWqzhSzTag== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudejtddgkeeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeevveefveefueegffekkefhgeeiueffgfetueejvdfg hfeujeejhfekjeelgedtgeenucfkphepjeehrddujedvrddurdeileenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghv vghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.4] (75-172-1-69.tukw.qwest.net [75.172.1.69]) by mail.messagingengine.com (Postfix) with ESMTPA id 52529240059; Sat, 5 Dec 2020 11:02:48 -0500 (EST) Subject: Re: Handling (None,) Query Results To: Hagen Finley , psycopg@lists.postgresql.org, psycopg@postgresql.org References: <5f75fbb2-6da2-60f3-5488-9013d1328361@datasundae.com> From: Adrian Klaver Message-ID: <80505104-1778-6157-cb9f-8cbafb748660@aklaver.com> Date: Sat, 5 Dec 2020 08:02:47 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 MIME-Version: 1.0 In-Reply-To: <5f75fbb2-6da2-60f3-5488-9013d1328361@datasundae.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 12/5/20 7:57 AM, Hagen Finley wrote: > Hello, > > I was thinking ‘finally, something I know how to do’ but alas simple > sum(revenue) where select statements in psycopg2 have proven to be more > complex than I imagined. > > First, there’s the Decimal tuple parsing which I can do (albeit somewhat > unnaturally)  (Decimal('450992.10'),) > > cur.execute("SELECT SUM(revusd) FROM sfdc where saccount = 'Big Company' AND stage > LIKE 'Commit%';") > commitd1 = cur.fetchone() > conn.commit() > commitd2 = commitd1[0] > > > if type(commitd2)is not None:commit =int(commitd2) > > else: > commit =0 > > 450992.10 > > > > If there is a better way to get to int I'd be all ears. > > > Second, there’s the NoneType  (None,) result from queries with no values. > > cur.execute("SELECT SUM(revusd) FROM sfdc where saccount = 'Big Company' AND stage > LIKE 'Win%';") > wind1 = cur.fetchone() > conn.commit() > wind2 = wind1[0] > > > if type(wind2)is int:win =int(wind2) > > else: > win =0 > > My goal is to return 0.00 when there are no results and an int when > there are results using the same code. Right now my if statements are > different: > > if type(commitd2)is not None: > > if type(wind2)is int: > > Possibly ignoring my fledgling attempts to solve this problem, is there > a simple method by which people convert the: > > 1. (Decimal('450992.10'),) to a 450992.10? > > 2. (None,) to 0.00? > >  Thanks for your thoughts on this question. Just do it in the query: "SELECT COALESCE(SUM(revusd, 0)) FROM sfdc where saccount = 'Big Company' AND stage LIKE 'Commit%';" If SUM(revusd) is NULL then COALESCE will substitute 0. > > Best, > > Hagen Finley > > Fort Collins, CO -- Adrian Klaver adrian.klaver@aklaver.com