Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I have found your site brilliant. What makes it good are the people that contribute to the site..."

Geography

Where in the world do Tek-Tips members come from?
abenstex (Programmer)
1 Aug 06 4:46
Hi everyone,

i have amongst others the columns pid(varchar) and insertDate(timestamp) and would like to obtain all distinct pid values ordered by insertDate. The problem, though, is that with the following query

CODE

select distinct pid, insertDate from mytable order by insertDate
returns all distinct combinations for pid and insertDate. But i want ONLY the distinct pids ordered by the timestamp.

Any advice is really appreciated.
ceco (IS/IT--Management)
1 Aug 06 5:03
try SELECT DISTINCT ON (pid) pid, insertDate ....

see for details
http://www.postgresql.org/docs/8.1/interactive/sql-select.html
abenstex (Programmer)
1 Aug 06 5:20
I tried that but it seems not to work with order by. That's the error i get when i include the order by:
SELECT DISTINCT ON expressions must match initial ORDER BY expressions
feherke (Programmer)
1 Aug 06 6:05
Hi

As far as I remember, this was my first question on Tek-Tips in thread699-434666. smile

But since then, I did not find out the solution. The best workaround is using sub-select :

CODE

select * from (
  select distinct on (pid) pid, insertDate from mytable
) foo order by insertDate

Feherke.
http://rootshell.be/~feherke/

abenstex (Programmer)
1 Aug 06 6:18
Thanks that worked!!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close