X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 6599E9DC879 for ; Fri, 17 Feb 2006 19:56:53 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 90046-09 for ; Fri, 17 Feb 2006 19:56:56 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 6D4C59DC869 for ; Fri, 17 Feb 2006 19:56:51 -0400 (AST) Received: from colibri.its.uu.se (colibri.its.uu.se [130.238.4.154]) by svr4.postgresql.org (Postfix) with ESMTP id 24D225AF076 for ; Fri, 17 Feb 2006 23:56:55 +0000 (GMT) Received: by colibri.its.uu.se (Postfix, from userid 211) id A83AEF3AF; Sat, 18 Feb 2006 00:56:52 +0100 (NFT) Received: from colibri.its.uu.se(127.0.0.1) by colibri.its.uu.se via virus-scan id s14344; Sat, 18 Feb 06 00:56:49 +0100 Received: from [130.243.238.206] (nl102-238-206.student.uu.se [130.243.238.206]) by colibri.its.uu.se (Postfix) with ESMTP id 15A9514E for ; Sat, 18 Feb 2006 00:56:49 +0100 (NFT) Message-ID: <43F662C4.9070402@gmail.com> Date: Sat, 18 Feb 2006 00:56:52 +0100 From: Jan Danielsson User-Agent: Thunderbird 1.5 (Windows/20051201) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: two count columns? X-Enigmail-Version: 0.94.0.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig6CB8AF66DE4F5E40D8078905" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.08 required=5 tests=[AWL=0.080] X-Spam-Score: 0.08 X-Spam-Level: X-Archive-Number: 200602/221 X-Sequence-Number: 24231 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig6CB8AF66DE4F5E40D8078905 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hello all, I have written a small firewall, and sshd, logging system which uses postgresql for storing the logs. The table that stores the firewall logs has, among other columns, from_ip, port and a timestamp. I want to construct a query which returns the columns: from_ip | port 22 entries | total entries | last_access I have managed to put this together: select from_ip, count(from_ip) as entries, max(ts)::timestamp(0) as last_access from log where to_port=3D22 and direction=3D'in' group by fro= m_ip So the only thing I'm missing is the total number of log entries matching the from_ip, but that's where I'm stuck. My instinct is to try to use subqueries: select from_ip, count(from_ip) as entries, count(select * from log where =2E..) as tot_entries, max(ts)::timestamp(0) as last_access from log wher= e to_port=3D22 and direction=3D'in' group by from_ip =2E.but count() doesn't take a subquery afaik, and how would I match the from_ip in the inner select with the outer one? So I assume that subqueries aren't the way yo go. Is there even a query to return those columns in that configuration? Thankful for any hints or tips. --=20 Kind Regards, Jan Danielsson Te audire non possum. Musa sapientum fixa est in aure. --------------enig6CB8AF66DE4F5E40D8078905 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.1 (MingW32) iD8DBQFD9mLE8wBCTJQ8HEIRAoo+AJ44yB6WRXo/cvUGomP1aI4q9tOWbgCfT9kC sq+iZ5HXTrjm0X/RwGWIkmE= =gkbm -----END PGP SIGNATURE----- --------------enig6CB8AF66DE4F5E40D8078905--