Re: counting query

From: garry saddington <garry(at)schoolteachers(dot)co(dot)uk>
To: Ron Johnson <ron(dot)l(dot)johnson(at)cox(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: counting query
Date: 2007-01-28 21:18:50
Message-ID: 1170019130.10217.3.camel@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, 2007-01-28 at 09:57 -0600, Ron Johnson wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 01/28/07 07:05, garry saddington wrote:
> > I have a table definition such as:
> >
> > CREATE TABLE attendance
> > (
> > attendanceid serial primary key,
> > entered date DEFAULT current_date NOT NULL,
> > absent boolean,
> > authorization text default 'N',
> > timeperiod char(2) check(timeperiod in('AM','PM')),
> > days varchar(10),
> > studentid int,
> > unique(entered,timeperiod,studentid)
> > )
> >
> > Which is used to record school attendance data. I am now trying to write
> > a query to identify trends in absences by counting the days column and
> > returning any student that has repeated absences on certain days. I am
> > struggling to return anything that does not need further manipulation in
> > Python before being useful.
> > Does anyone have any ideas?
>
> When you say "certain days", you mean "days of the week"?
>
> If so, create a view like:
> CREATE VIEW V_DAY_ABSENCES AS
> SELECT ENTERED,
> AUTHORIZATION,
> TIMEPERIOD,
> DAYS,
> STUDENTID,
> DOW(CAST(ENTERED AS TIMESTAMP)) AS WEEKDAY
> FROM ATTENDANCE
> WHERE ABSENT = TRUE;
>
> Then, this query should do what you want:
> SELECT STUDENTID,
> TIMEPERIOD,
> WEEKDAY,
> COUNT(*)
> FROM V_DAY_ABSENSES
> GROUP BY STUDENTID,
> TIMEPERIOD,
> WEEKDAY
> HAVING COUNT(*) > 3;
Thank you, this works great. But I have another problem: Is it possible
to identify absences in consecutive weeks on the same day. EG. If a
pupil has a pattern of having every monday AM off school, how could that
be identified?
Regards
Garry

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-01-28 21:28:15 Re: "explain analyse" much slower than actual query
Previous Message Phil Endecott 2007-01-28 20:24:59 Re: "explain analyse" much slower than actual query