From: A. Kretschmer <andreas.kretschmer@schollglas.com>
To: pgsql-sql@postgresql.org
Subject: Re: group by day
Date: Thu, 24 May 2007 21:42:42 +0200
Message-ID: <20070524194242.GA21054@a-kretschmer.de> (raw)
In-Reply-To: <04c201c79e34$4e3877c0$143c520a@ntc2s.comsquared.com>
References: <04c201c79e34$4e3877c0$143c520a@ntc2s.comsquared.com>
am Thu, dem 24.05.2007, um 14:49:47 -0400 mailte Edward W. Rouse folgendes:
> I have an audit table that I am trying to get a count of the number of distinct
> entries per day by the external table key field. I can do a
>
> select count(distinct(id)) from audit where timestamp >= '01-may-2007'
>
> and get a total count. What I need is a way to group on each day and get a
> count per day such that the result would be something like
>
> date count
> 01-may-2007 107
> 02-may-2007 215
> 03-may-2007 96
> 04-may-2007 0
>
>
> I would prefer the 0 entries be included but can live without them. Thanks.
You are searching for GROUP BY.
A simple example:
test=*# select * from foo;
ts | val
---------------------+-----
2007-05-01 08:00:00 | 10
2007-05-01 08:00:00 | 20
2007-05-02 10:00:00 | 20
2007-05-02 11:00:00 | 30
(4 rows)
Time: 1.079 ms
test=*# select ts::date, sum(val) from foo group by 1;
ts | sum
------------+-----
2007-05-02 | 50
2007-05-01 | 30
(2 rows)
Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: andreas.kretschmer@schollglas.com
Subject: Re: group by day
In-Reply-To: <20070524194242.GA21054@a-kretschmer.de>
* 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