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 1ib468-0002GI-EG for pgsql-general@arkaria.postgresql.org; Sat, 30 Nov 2019 14:51:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ib465-0001J5-In for pgsql-general@arkaria.postgresql.org; Sat, 30 Nov 2019 14:51:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ib463-0001Iy-2u for pgsql-general@lists.postgresql.org; Sat, 30 Nov 2019 14:51:44 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ib45v-0005y4-45 for pgsql-general@postgresql.org; Sat, 30 Nov 2019 14:51:41 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id B93AE2274C; Sat, 30 Nov 2019 09:51:33 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 30 Nov 2019 09:51:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=3 3suTv/wD0LIG9jU2NRVFIyYExH8UKQIrSHq9tRAPbA=; b=Cu9SQnyI0pOoXd6LY Cz/apDNM+95YgDTZGEM7jjzxRAO5Ox1VTdlFVMoAZZAG+CUs1RVizxDYsmqBq9M2 PZHAG/hXCwexJldkexeCwY2bbb1dOdGAYAFJZuW1etJDrAf3mKrtI7/2Bxapc1ap 5IC9CYksMaqnX2nYYezovEYoQtwWe7GXlQ9WyaHearP7QJyqegxx5qOJ6jv9OBYf iMVzHXIvoAJUNhzMDC8rkCRhjlHgoQWVoqHuObk8MVV6RahYQuJdW14BQQuMVzUr M9y3yn9tj+/xnS6D+nhWPvs2iRj4sDGBIcxNAN5xc3A6Yv0F0q/wskW3Xk02ziMr 7vUyw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=33suTv/wD0LIG9jU2NRVFIyYExH8UKQIrSHq9tRAP bA=; b=pTwQ+Uq749fPdsdhIUPNoCRQfj9JGEyZS3WKWkjmubNODZDv0aoqQVcyS zCdhFGMu6DE+rsuKUUGyCeJx/spNj+s1G/WR+e5pn5KJpBIpfodbVDzoEelVr4lg TLnyULLKgspS2+zolmykj1i5V+2Uv1gfBOMgmPT0gnFL+yKIWdhiEEO6OmvtA+j7 bFgRR4Za74+x9QERAt06sVpzvRHA9y/PdJ8L+8uElDBTGp8nrN1+CYTm37XyyKuP ZNzVgXs48wq1TsqyzXTQx47FFs1et37gLVmqmr1JM7d3TFC8xxYS9IwCkGWqKXuW 4nO8l/Ed3LVDlzM37yCQLLqEUFv5g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudejuddgjedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphepudejgedrvddurdeikedrvdegfeenucfrrghrrghmpehmrghilhhfrhho mheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvg hrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.2] (174-21-68-243.tukw.qwest.net [174.21.68.243]) by mail.messagingengine.com (Postfix) with ESMTPA id E22CA80059; Sat, 30 Nov 2019 09:51:32 -0500 (EST) Subject: Re: Counting booleans in GROUP BY sections To: Alexander Farber Cc: pgsql-general References: <082dc4bf-e668-f091-9d96-9725dd5ddfee@aklaver.com> From: Adrian Klaver Message-ID: <8580a1cd-7a65-9dd1-50eb-d446aa8ea5c9@aklaver.com> Date: Sat, 30 Nov 2019 06:51:32 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 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/30/19 4:08 AM, Alexander Farber wrote: > My context is that I have a table of player moves with PK mid (aka "move > id"). > > And I am able to find "interesting" moves by the high score or all 7 > letter tiles used. > > But I do some human reviewing and set a "puzzle" boolean for truly > interesting moves. > > For the reviewing tool I would like to display headers: a "Mon YYYY" > plus the number of true puzzles per section. > > Thanks to David's hint the following seems to work even though I wonder > if it is the most optimal way to call TO_CHAR twice: Given that played contains values, I assume, that are at multiple points in a month and you want the 'group' to be a month it looks alright to me. Though if it bothers you then another option is date_trunc(): test=# select date_trunc('month', '11/02/2019 13:00'::timestamp), date_trunc('month', '11/23/2019 13:00'::timestamp); date_trunc | date_trunc ---------------------+--------------------- 11/01/2019 00:00:00 | 11/01/2019 00:00:00 > > 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, >                                                       -- used for header >         COUNT(NULLIF(puzzle, FALSE)) OVER (PARTITION BY TO_CHAR(played, > 'Mon YYYY')), --used for header >         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 >     ORDER BY played DESC > $func$ LANGUAGE sql STABLE; > > Regards > Alex > > P.S: Below is my table description again and the output of the above > function: > > 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 > > 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  |         1 | f          | 1331343 | > 78467b5f3bde3d3f2291cf539c949f79 |   46134 |        28 >  Nov 2018  |         1 | f          | 1326876 | > e4928d3c34f50b8e6eabf7bad5b932fe |   46007 |        28 >  Nov 2018  |         1 | f          | 1324466 | > 6228ba509a7124f485feb5c1acbb6b68 |   45922 |        26 >  Nov 2018  |         1 | f          | 1322050 | > b67b091d383678de392bf7370c735cab |   45877 |        34 >  Nov 2018  |         1 | f          | 1320017 | > 35f03b0c7159cec070c00aa80359fd42 |   44255 |       120 > ..... >  May 2018  |         3 | f          |   95114 | > e7e8bab64fab20f6fec229319e2bab40 |    7056 |        28 >  May 2018  |         3 | f          |   88304 | > 161c0638dede80f830a36efa6f428dee |    6767 |        40 >  May 2018  |         3 | f          |   86180 | > 4d47a65263331cf4e2d2956886b6a72f |    6706 |        26 >  May 2018  |         3 | f          |   85736 | > debb1efd673c91947a8aa7f38be4217c |    6680 |        28 >  May 2018  |         3 | f          |   82522 | > e55ec68a5a5dacc2bc463e397198cb1c |    6550 |        27 >  Apr 2018  |         0 | f          |   78406 | > f5d264ccfe94aaccd90ce6c019716d4d |    5702 |        58 >  Apr 2018  |         0 | f          |   77461 | > 404886e913b698596f9cf3648ddf6fa4 |    1048 |        26 > (415 rows) -- Adrian Klaver adrian.klaver@aklaver.com