Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iakOC-0004uZ-JN for pgsql-general@arkaria.postgresql.org; Fri, 29 Nov 2019 17:49:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iakOB-00031E-Cx for pgsql-general@arkaria.postgresql.org; Fri, 29 Nov 2019 17:49:07 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1iakKw-000780-Bp for pgsql-general@lists.postgresql.org; Fri, 29 Nov 2019 17:45:46 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iakKs-0006AK-DO for pgsql-general@postgresql.org; Fri, 29 Nov 2019 17:45:45 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id B12EB22A6B; Fri, 29 Nov 2019 12:45:39 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 29 Nov 2019 12:45:39 -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=y Zo1Im6P9BKY8FT7kAdSms8rDJcghxQ5AIlOl0fMgD0=; b=vzVhL5jHsFoOr+Df3 C0n+evxLCQbCX8lRSjaX6BKsFTwF/kNqbU66wzUT4qZ/mdASPe9fcYZ843dwKr3V De5TiRjaZPDLNy2rra9jRr2Ic6WcKNdSSOp1qn6fvmwtf/5cp9ehu/4X8fXtWS6U xW6PXGYjIdqlne+OxwQ+leCJGosB/CQHkijkV34xdlj+k+D5BX02R+tZoatj8l7V 8tWnqC8AwPs+RsHJd/9XnoPnDF7olN5kUY2W94nq9hdxP10gWMtVTzzsM7BIIUvF 148YoNBedEFgaFuQ/GP2xHPmEU29jzmvcEGdEde4FRUC3nMs7DEMhWqr7n10YpAp ePvCg== 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=yZo1Im6P9BKY8FT7kAdSms8rDJcghxQ5AIlOl0fMg D0=; b=gJz6f9r2qRv3VuhVHHcMQ3rSAmJUrf9lcEhsGKZjqestREfEUZNuc3GtK wHZli4mr/37bfdLmtC6W+fuy2eD6uvrqJPoJhtA/vcis/1/+XKvpISruOq+4st3P yxEDf/s5SbTSQWDWJ58epIH/jypB3BFmGGHLlGey1h9/vxA02B9oR76yfIjUcpdt K4fC2zUd/T+0FukJkh2cQHCoGooqb9qUnJ8+aZIKXgLvS0bTKs4HF3/NjL0tEpsV amO+eoh+iZxV6tQVdaGy/fVQdx8K95E18le/8ImjSNL6a3Ry1W4kwELgGPUFgD1d 5+bd0DbVVkzxndTBcsqHmszQUX55Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudeiledguddtudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecukfhppedujedvrdehkedrfedtrddufeefnecurfgrrhgrmhepmhgrihhlfhhr ohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsth gvrhfuihiivgeptd X-ME-Proxy: Received: from ranger.suse (unknown [172.58.30.133]) by mail.messagingengine.com (Postfix) with ESMTPA id 45BF180061; Fri, 29 Nov 2019 12:45:38 -0500 (EST) Subject: Re: Counting booleans in GROUP BY sections To: Alexander Farber , pgsql-general References: From: Adrian Klaver Message-ID: Date: Fri, 29 Nov 2019 09:45:33 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.1 MIME-Version: 1.0 In-Reply-To: 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 11/29/19 8:38 AM, Alexander Farber wrote: > Good evening, > > I am trying to count the booleans per each GROUP BY section by the > following stored function: > > CREATE OR REPLACE FUNCTION words_list_puzzles( >                 in_start interval, >                 in_end interval > >         ) RETURNS TABLE ( >                 out_label  text, >                 out_count  bigint, >                 out_puzzle boolean, >                 out_mid    bigint, >                 out_secret text, >                 out_gid    integer, >                 out_score  integer >         ) AS > $func$ > >     SELECT >         TO_CHAR(played, 'Mon YYYY') AS label, >         COUNT(NULLIF(puzzle, FALSE)), -- this only returns 0 or 1, why? If I am following it is because you have mid in GROUP BY and mid is a PK. Since mid will always be unique you will have at most on row per group. >         puzzle, >         mid, >         MD5(mid || ‘my little secret’) AS secret, >         gid, >         score > >     FROM words_moves >     WHERE action = 'play' >     AND LENGTH(hand) = 7 >     AND (LENGTH(letters) = 7 OR score > 90) >     AND played > CURRENT_TIMESTAMP - in_start >     AND played < CURRENT_TIMESTAMP - in_end >     GROUP BY label, puzzle, mid, secret, gid, score >     ORDER BY played DESC > > $func$ LANGUAGE sql STABLE; > > But when I run it, I only get 0 or 1 in the out_count column: > > words_ru=> select * from words_list_puzzles(interval '2 year', interval > '1 year'); >  out_label | out_count | out_puzzle | out_mid |            out_secret >          | out_gid | out_score > -----------+-----------+------------+---------+----------------------------------+---------+----------- >  Nov 2018  |         0 | f          | 1326876 | > e4928d3c34f50b8e6eabf7bad5b932fe |   46007 |        28 >  Nov 2018  |         0 | f          | 1324466 | > 6228ba509a7124f485feb5c1acbb6b68 |   45922 |        26 >  Nov 2018  |         0 | f          | 1322050 | > b67b091d383678de392bf7370c735cab |   45877 |        34 >  Nov 2018  |         0 | f          | 1320017 | > 35f03b0c7159cec070c00aa80359fd42 |   44255 |       120 >  Nov 2018  |         0 | f          | 1319160 | > 83df42f7ad398bbb060fc02ddfdc62c0 |   45031 |        95 > ..... >  May 2018  |         0 | f          |  264251 | > 2fff1154962966b16a2996387e30ae7f |   10946 |        99 >  May 2018  |         1 | t          |  257620 | > 645613db6ea40695dc967d8090ab3246 |   12713 |        93 >  May 2018  |         0 | f          |  245792 | > bb75bfd9cb443ff541b199d893c68117 |   12359 |        24 >  May 2018  |         1 | t          |  243265 | > d899a5d642ccd96d931194f48ef56d53 |   11950 |       123 >  May 2018  |         0 | f          |  231953 | > ad53b5b2c0d4cced3d50e8b44ad53e55 |   11910 |        32 > > - while I was hoping to get 2 for the "May 2018" section. > > What am I doing wrong please, why don't the values add up? Below is the > table desc: > > words_ru=> \d words_moves >                                       Table "public.words_moves" >  Column  |           Type           | Collation | Nullable | >       Default > ---------+--------------------------+-----------+----------+------------------------------------------ >  mid     | bigint                   |           | not null | > nextval('words_moves_mid_seq'::regclass) >  action  | text                     |           | not null | >  gid     | integer                  |           | not null | >  uid     | integer                  |           | not null | >  played  | timestamp with time zone |           | not null | >  tiles   | jsonb                    |           |          | >  score   | integer                  |           |          | >  letters | text                     |           |          | >  hand    | text                     |           |          | >  puzzle  | boolean                  |           | not null | false > Indexes: >     "words_moves_pkey" PRIMARY KEY, btree (mid) >     "words_moves_gid_played_idx" btree (gid, played DESC) >     "words_moves_uid_action_played_idx" btree (uid, action, played) >     "words_moves_uid_idx" btree (uid) > Check constraints: >     "words_moves_score_check" CHECK (score >= 0) > Foreign-key constraints: >     "words_moves_gid_fkey" FOREIGN KEY (gid) REFERENCES > words_games(gid) ON DELETE CASCADE >     "words_moves_uid_fkey" FOREIGN KEY (uid) REFERENCES > words_users(uid) ON DELETE CASCADE > Referenced by: >     TABLE "words_scores" CONSTRAINT "words_scores_mid_fkey" FOREIGN KEY > (mid) REFERENCES words_moves(mid) ON DELETE CASCADE > > Thank you > Alex > -- Adrian Klaver adrian.klaver@aklaver.com