Re: SELECT DISTINCT problems

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: SCAHILL KEVIN <9726209(at)student(dot)ul(dot)ie>
Cc: "'pgsql-sql(at)postgreSQL(dot)org'" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: SELECT DISTINCT problems
Date: 2001-02-28 15:35:00
Message-ID: 18169.983374500@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

SCAHILL KEVIN <9726209(at)student(dot)ul(dot)ie> writes:
> I have tried this but it does not work:
> Set rsLecturers = Server.CreateObject("ADODB.Recordset")
> sqlLect = "Select LecturerName, MIN(ProjectCode) from tblSuggestions WHERE
> LecturerName IN ( SELECT DISTINCT LecturerName FROM tblSuggestions)"
> rsLecturers.Open sqlLect, Conn, 3, 3

You seem to be trying to re-invent the notion of GROUP BY. The correct
way to write this sort of query in SQL is

Select LecturerName, MIN(ProjectCode) from tblSuggestions GROUP BY LecturerName

This gives you one output row for each distinct value of LecturerName,
and within that row the MIN() aggregates over all the original rows that
have that LecturerName. See

http://www.postgresql.org/devel-corner/docs/postgres/query-agg.html
http://www.postgresql.org/devel-corner/docs/postgres/queries.html#QUERIES-GROUP

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2001-02-28 15:59:29 Re: SELECT DISTINCT problems
Previous Message Andrew Perrin 2001-02-28 14:49:01 Re: create function w/indeterminate number of args?