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.
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
Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.
Red Flag Submitted
Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.
Reply To This Thread
Posting in the Tek-Tips forums is a member-only feature.
RE: SELECT DISTINCT and ORDER BY
ceco (IS/IT--Management)see for details
ht
RE: SELECT DISTINCT and ORDER BY
abenstex (Programmer)SELECT DISTINCT ON expressions must match initial ORDER BY expressions
RE: SELECT DISTINCT and ORDER BY
feherke (Programmer)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
select distinct on (pid) pid, insertDate from mytable
) foo order by insertDate
Feherke.
http://rootshell.be/~feherke/
RE: SELECT DISTINCT and ORDER BY
abenstex (Programmer)Red Flag This Post
Red Flag Submitted
Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.
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! Already a Member? Login
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.