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 1klZvu-0003yT-5o for psycopg@arkaria.postgresql.org; Sat, 05 Dec 2020 15:57:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1klZvs-00084B-W4 for psycopg@arkaria.postgresql.org; Sat, 05 Dec 2020 15:57:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1klZvs-000844-Qk for psycopg@lists.postgresql.org; Sat, 05 Dec 2020 15:57:12 +0000 Received: from p3plsmtpa06-10.prod.phx3.secureserver.net ([173.201.192.111]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1klZvq-0000tN-6L for psycopg@postgresql.org; Sat, 05 Dec 2020 15:57:12 +0000 Received: from [192.168.1.54] ([98.38.100.21]) by :SMTPAUTH: with ESMTPSA id lZvmkT49DhuAjlZvnknIAX; Sat, 05 Dec 2020 08:57:07 -0700 X-CMAE-Analysis: v=2.4 cv=FaHyeLy6 c=1 sm=1 tr=0 ts=5fcbadd3 a=tZXnaF/PLMojNGgMD7Zm7A==:117 a=tZXnaF/PLMojNGgMD7Zm7A==:17 a=r77TgQKjGQsHNAKrUKIA:9 a=d1IvFtyfoKIgwmjGrcMA:9 a=QEXdDO2ut3YA:10 a=e93cgQTOmEHV7aDFa8kA:9 a=un7o5G-sgRqq4JAh:21 a=_W_S_7VecoQA:10 X-SECURESERVER-ACCT: hagen@datasundae.com To: psycopg@lists.postgresql.org, psycopg@postgresql.org From: Hagen Finley Subject: Handling (None,) Query Results Message-ID: <5f75fbb2-6da2-60f3-5488-9013d1328361@datasundae.com> Date: Sat, 5 Dec 2020 08:57:06 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------4BC0DC95258D5F1185AFA1CE" Content-Language: en-US X-CMAE-Envelope: MS4xfAqQDlzWigKCcFpLWAr/CPqKGTxWC5Ag5mN+H2yb9K3So/PwsACq+cJvGBXtkEyN+VMu8R1BM6HQW24A8HBYkUWuSdO1yKSZShSoomYH08b6CsWRXca4 ABkmhWIeUBXMYmHZh/nH9MXcrZ9efdLarOGLXcVm/IfkC1GuP/N6RJoN4xMkkX8nizX7f25z9Q6pMxv7JmswQm91VYTFmpmENsqpXIijer6PsrGaYlFNmzWC List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------4BC0DC95258D5F1185AFA1CE Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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. Best, Hagen Finley Fort Collins, CO --------------4BC0DC95258D5F1185AFA1CE Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

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

<class 'int'>

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 <class 'int'> 450992.10?

2. (None,) to 0.00?

 Thanks for your thoughts on this question.

Best,

Hagen Finley

Fort Collins, CO --------------4BC0DC95258D5F1185AFA1CE--