tags:

views:

42

answers:

2

Let's say I have a table with two columns, id and hash:

id     | hash
------------------
1      | bb
2      | aa
3      | aa
4      | bb

I need to order them by id (descending), yet group all the rows that have the same value. An algorithm that did this would be for example:

  • Gather into disjunct subsets the table so that the columns for which hash is the same are together.
  • Sort the subsets by the their maximum id, descending.
  • The subsets rows may optionally be sorted by id, descending.

The result would be

id     | hash
------------------
4      | bb
1      | bb
3      | aa
2      | aa

Thanks.

+1  A: 

Would something like this be of any use?...

SELECT *
    FROM my_table MT1
    ORDER BY (SELECT MAX(id)
                  FROM my_table MT2
                  WHERE MT2.hash = MT1.hash) DESC,
             id DESC;
Brian Hooper
+2  A: 

In Postgres 8.4...(order by number is just an alias for the column to order by)

select id,hash, max(id) over (partition by hash) h
   from my_table order by 3 desc,1 desc;

OR

select id,hash
from my_table order by max(id) over (partition by hash) desc,
id desc
rfusca
Thanks, this seems better than Brian's solution, which possibly generates a lot of subqueries.
Artefacto
I actually had to then also order by hash. Each id actually could have more than one hash (the ids represent files, but these files could contain other files if they were zip or rar archives; the query's purpose is to group duplicates from the most recent). This was not a requirement, so it's really my fault :p
Artefacto