Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT DISTINCT and ORDER BY

Status
Not open for further replies.

abenstex

Programmer
Jan 9, 2005
47
DE
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.
 
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
 
Hi

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

But since then, I did not find out the solution. The best workaround is using sub-select :
Code:
[b]select[/b] * [b]from[/b] (
  [b]select distinct on[/b] (pid) pid, insertDate [b]from[/b] mytable
) foo [b]order by[/b] insertDate

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top