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 1gziAt-0004ez-OE for pgsql-sql@arkaria.postgresql.org; Fri, 01 Mar 2019 13:26:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gziAs-0000xt-21 for pgsql-sql@arkaria.postgresql.org; Fri, 01 Mar 2019 13:26:02 +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 1gziAr-0000xm-Nn for pgsql-sql@lists.postgresql.org; Fri, 01 Mar 2019 13:26:01 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gziAo-0001lr-Dt for pgsql-sql@lists.postgresql.org; Fri, 01 Mar 2019 13:26:00 +0000 Received: from [192.168.127.1] (port=58072 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gziAl-000CGG-Ez; Fri, 01 Mar 2019 13:25:55 +0000 Received: from [127.0.0.1] (port=11356 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gziAk-000LsP-MR; Fri, 01 Mar 2019 13:25:54 +0000 From: Andrew Gierth To: Rocky Ji Cc: pgsql-sql@lists.postgresql.org Subject: Re: Why does GROUP BY reduce number of rows? In-Reply-To: (Rocky Ji's message of "Fri, 1 Mar 2019 18:25:44 +0530") Message-ID: <87y35yd9ul.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 01 Mar 2019 13:25:53 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Rocky" == Rocky Ji writes: Rocky> Let's call this error/mistake a "UDFS" (Unintended Deviation Rocky> From Standards). What deviation? If the standard leaves something unspecified, the PostgreSQL can choose to do as it pleases. (We can also choose to just violate the standard...) Rocky> I think: reduction in number of rows in output when `GROUP BY` Rocky> clause is used is another UDFS. Reducing the number of rows of output is the SOLE PURPOSE AND REASON TO EXIST of the GROUP BY clause. By definition, the clause produces only one output row for each matching group in the input. The rows produced by GROUP BY are also not "the final row" or even "the first row" of the group. They are _new_ rows computed from column expressions which must reference the input in only two ways: - aggregate functions computed over the input group - values which can be proved by the implementation to be guaranteed to be the same in all rows of the input group (that is, the grouping columns themselves and anything the implementation can prove is a functional dependency of the grouping columns). Some other databases may be lax about enforcing these restrictions and allow non-dependent columns to be included in the result, taking their values from some arbitrary row of the input group. PG if anything is _too_ strict about this, since for implementation reasons we can prove functional dependency only in very limited cases and we reject anything else. Rocky> What if important (i.e. "uniquely identifying" for "human eyes") Rocky> data was contained in first row of a group. Why does the Rocky> underlying program (psql server, I suppose) **decide** that Rocky> preceding rows are not worth displaying? The person who wrote GROUP BY in the query made that decision. BTW, if you want to calculate aggregated values across rows without reducing the number of rows of output, there's an entirely separate mechanism for that: window functions. -- Andrew (irc:RhodiumToad)