Re: Selecting distinct records

From: "David Busby" <busby(at)pnts(dot)com>
To: "Dave" <dave(at)hawk-systems(dot)com>, <pgsql-php(at)postgresql(dot)org>
Subject: Re: Selecting distinct records
Date: 2002-11-21 19:02:05
Message-ID: 006901c29190$7beea230$4000000a@busbydev
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php

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(at)hawk-systems(dot)com>
To: <pgsql-php(at)postgresql(dot)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(at)postgresql(dot)org

In response to

Responses

Browse pgsql-php by date

  From Date Subject
Next Message Dave [Hawk-Systems] 2002-11-21 21:26:55 Re: Selecting distinct records
Previous Message Dave 2002-11-21 18:43:15 Re: Selecting distinct records