From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Paul Linehan <linehanp@tcd.ie>
To: Todd Sykes <toddsykes@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: SQL problem (forgot to change header with earlier post!).
Date: Tue, 29 May 2018 06:59:40 -0700
Message-ID: <ef5956ad-23ce-bb28-fb48-75b71953e740@aklaver.com> (raw)
In-Reply-To: <49c4af45-029c-f6df-b470-64008ab50c67@aklaver.com>
References: <CAF4RT5S7AJ-Vt9KKL2vuJyu7M7yRbDFjbUWPsFFGTkW74kigkw@mail.gmail.com>
<CAPrw1UmM2bnyozDaaExQF+qU8x5ShMB3+R2PPePhPTuet+Sjdw@mail.gmail.com>
<CAF4RT5T13GOrq6_YLeXxJwo9GamvSA0U8ZOUW+-vSe8WwTqq5Q@mail.gmail.com>
<CAPrw1Um0yQSNUApJz6kCyZ-w3bkJjmOfPpSMqEDzV50YO_1Rjw@mail.gmail.com>
<CAF4RT5QAQxH+WvF+c7PRfM4AK=TbDwD9Egvi3Z58-LXXrNFu9g@mail.gmail.com>
<49c4af45-029c-f6df-b470-64008ab50c67@aklaver.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, linehanp@tcd.ie, toddsykes@gmail.com
Subject: Re: SQL problem (forgot to change header with earlier post!).
In-Reply-To: <ef5956ad-23ce-bb28-fb48-75b71953e740@aklaver.com>
References: <CAF4RT5S7AJ-Vt9KKL2vuJyu7M7yRbDFjbUWPsFFGTkW74kigkw@mail.gmail.com> <CAPrw1UmM2bnyozDaaExQF+qU8x5ShMB3+R2PPePhPTuet+Sjdw@mail.gmail.com> <CAF4RT5T13GOrq6_YLeXxJwo9GamvSA0U8ZOUW+-vSe8WwTqq5Q@mail.gmail.com> <CAPrw1Um0yQSNUApJz6kCyZ-w3bkJjmOfPpSMqEDzV50YO_1Rjw@mail.gmail.com> <CAF4RT5QAQxH+WvF+c7PRfM4AK=TbDwD9Egvi3Z58-LXXrNFu9g@mail.gmail.com> <49c4af45-029c-f6df-b470-64008ab50c67@aklaver.com> <ef5956ad-23ce-bb28-fb48-75b71953e740@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox