pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Busby <busby@pnts.com>
To: Dave <dave@hawk-systems.com>
To: pgsql-php@postgresql.org
Subject: Re: Selecting distinct records
Date: Thu, 21 Nov 2002 11:02:05 -0800
Message-ID: <006901c29190$7beea230$4000000a@busbydev> (raw)
References: <DBEIKNMKGOBGNDHAAKGNMEGGDPAB.dave@hawk-systems.com>

Dave,
    Try:
select distinct on (sessionid) sum(sessiontime) from logs
where name='joeblowuser' and datetime > 1036040400
group by sessionid.

sum() is an aggerate (sp?) function, that means it munges a field
(sessiontime) from multiple records into one field in one record.  Since you
are also selecting sessionid (from mulitple records) you need to munge it
some how, that munge is accomplished via 'group by'.  From your previous
e-mail it seems that (IMHO) the real problem is that duplicates are getting
inserted via external hardware interaction, this select might be a bandage
on a wound whose true size isn't known...

PostgreSQL docs:
Distinct:
  http://www.postgresql.org/idocs/index.php?queries-select-lists.html
Group By:
  http://www.postgresql.org/idocs/index.php?sql-select.html

/B



----- Original Message -----
From: "Dave" <dave@hawk-systems.com>
To: <pgsql-php@postgresql.org>
Sent: Thursday, November 21, 2002 10:43
Subject: Re: [PHP] Selecting distinct records


> >What I am looking to do is
> >- grab every record for $user
> >- remove any records that have identical ipaddress+sessionid+refid
> >- then sort the results by date_time or something else
>
> this last requirement is where the problem is...
>
> is you do a sum() or order by in the select statement you get and error,
for
> example;
>
> select distinct on (sessionid) sum(sessiontime) from logs where
> name='joeblowuser' and datetime > 1036040400;
>
> ERROR:  Attribute logs.sessionid must be GROUPed or used in an aggregate
> function
>
> Same if you have to order by datetime or something...
>
> Dave
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org




view thread (5+ messages)  latest in thread

Message-ID: <006901c29190$7beea230$4000000a@busbydev>
Permalink:  ../006901c29190$7beea230$4000000a@busbydev/
Also on:    postgresql.org/message-id/006901c29190$7beea230$4000000a@busbydev

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: busby@pnts.com, dave@hawk-systems.com
  Subject: Re: Selecting distinct records
  In-Reply-To: <006901c29190$7beea230$4000000a@busbydev>
  References: <DBEIKNMKGOBGNDHAAKGNMEGGDPAB.dave@hawk-systems.com> <006901c29190$7beea230$4000000a@busbydev>

* 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