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 1fNfAF-0007TQ-6F for pgsql-general@arkaria.postgresql.org; Tue, 29 May 2018 13:59:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fNfAA-0006q6-KW for pgsql-general@arkaria.postgresql.org; Tue, 29 May 2018 13:59:46 +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 1fNfAA-0006pw-CP for pgsql-general@lists.postgresql.org; Tue, 29 May 2018 13:59:46 +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 1fNfA7-0001QR-1k for pgsql-general@postgresql.org; Tue, 29 May 2018 13:59:45 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id C39CF21B82; Tue, 29 May 2018 09:59:41 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 29 May 2018 09:59:41 -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=iofXPzNJRBRGQ2gzLiXBAGSivZ5fL 1pSJooguweYPwM=; b=poMlumxrPebki14VjmEliPM8+hR+Xf/bZzigcl8Zx3TVR FmOz7B+P9kEHGdG5ZSqMcZHvk9prz3JhjIlMA1TYOMte/86SVDT/fNPlM4MvnAgj oXq8hNf9DFJMdDN/7xhpvQvW06fNz6lDU6IFEdWP1qGibfNLfS97QaDBaPFwBEmL 7lTzUtwYhBsj1+jaBAGKndJOeEexSTv1QEAKcOxQno0PjRmCBVU6MPsyXq56gG1F /+kgqKh9y2mjS/5eF2TiTZAzbxRcxbvDBW45I6LX3kwg/4Fkbi2jR188F/KAT+Oz Flzk6Hn9ngaEqGgN8GbVSYceMLvx/kaYnKR+c2kjw== 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=fm2; bh=iofXPz NJRBRGQ2gzLiXBAGSivZ5fL1pSJooguweYPwM=; b=RDlnPLQ5GfUgVJ/NP3dqg7 STyRysrcIj4ISOcw1YB6WX2eNSsPSbJqpISUMX518sF0EiLKaZDWX9sVkt0A34sx mJP5MIoM/LFCXabs8KWmmmOCjrmp6zxxGOuqJpLJyri/MFRoD7mUAw1u92gG9Xjk k2ws/XR1xpn59sZq3dGYWGCKCyR+mC3GA1AS8UfJsLPGBuc2Lcsnx3Uz6S6c5MVI +xCRIFqAAYcBjs1E11fQeu/ujUUusdcyOM6tAUd2+vhr0ADoNUdFkaxCoBrzhKcT Vy7vsdi5uNPwBLke1ka6W/tW1XYYebolouva6R4MolgGcUZ+RGODjbUFXbeC9ovw == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.159.209]) by mail.messagingengine.com (Postfix) with ESMTPA id 24354102C7; Tue, 29 May 2018 09:59:41 -0400 (EDT) Subject: Re: SQL problem (forgot to change header with earlier post!). To: Paul Linehan , Todd Sykes , pgsql-general@postgresql.org References: <49c4af45-029c-f6df-b470-64008ab50c67@aklaver.com> From: Adrian Klaver Message-ID: Date: Tue, 29 May 2018 06:59:40 -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: <49c4af45-029c-f6df-b470-64008ab50c67@aklaver.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 05/29/2018 06:52 AM, Adrian Klaver wrote: > On 05/29/2018 05:05 AM, Paul Linehan wrote: >> Hi again, and thanks for your efforts on my behalf! >> >>> WITH num AS >>> ( >>>     SELECT count (*) as cnt1 FROM v1 >>>   ), >>> div AS >>> ( >>>     SELECT count (*) as cnt2 FROM v2 >>>   ) >>>   SELECT (num.cnt1::numeric/div.cnt2) >>>  From num cross join div; >> >> >> I've tried running this code 4 different ways and none of them work - >> your original and my efforts to tweak the code! >> >> This always ends up giving just 1 (integer division - using float) or >> 1.0000000000 (using numeric). > > It would, each view has only a single row for the count value. From the > fiddle: > > SELECT * FROM v1; > > cnt1 > 13 > > SELECT * FROM v2; > > cnt2 > 11 > > So doing: > > SELECT count (*) as cnt1 FROM v1(2) > > is going to return 1 in both cases and 1/1 = 1. > > Change: > > SELECT count (*) as cnt1 FROM v1 > > SELECT count (*) as cnt2 FROM v2 > > to > > SELECT cnt1 FROM v1 > > SELECT cnt2 FROM v1 Cut and paste error, should be: SELECT cnt2 FROM v2 > >> >> Check out the fiddle here: >> https://dbfiddle.uk/?rdbms=postgres_10&fiddle=b1bd443baf16d85dee0436333a6fd919 >> >> >> >>> You could have also written it like your first statement without the >>> CTEs. >>> This way requires joining the tables with a cross or Cartesian join. >> >> Yes, the first statement is the way to go on this particular case, but >> I'm also trying to understand the ins and outs of CTEs, so I'm >> interesting in solving this one! >> >> >> Thanks again, >> >> >> Rgs, >> >> >> Pól... >> >> >> >>> Todd >> >> > > -- Adrian Klaver adrian.klaver@aklaver.com