Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Good luck getting help with a proprietary system. Unless the system is widely used or supports a very standardized SQL variant, you have almost no hope of getting an answer without getting it from the people who wrote the system.
–
Max VernonSep 26 '12 at 5:54

3 Answers
3

The easiest query to write is for MySQL (with not strict ANSI settings). It uses the non-standard construction:

SELECT key, value
FROM tableX
GROUP BY key ;

For other DBMSs, that have window functions (like Postgres, SQL-Server, Oracle, DB2), you can use them like this. The advantage is that you can select other columns in the result as well (besides the key and value) :

SELECT key, value
FROM tableX
( SELECT key, value,
ROW_NUMBER() OVER (PARTITION BY key
ORDER BY whatever) --- ORDER BY NULL
AS rn --- for example
FROM tableX
) tmp
WHERE rn = 1 ;

For older versions of the above and for any other DBMS, a general way that works almost everywhere. One disadvantage is that you cannot select other columns with this approach. Another is that aggregate functions like MIN() and MAX() do not work with some datatypes in some DBMSs (like bit, text, blobs):

@WilliamKF If by "chosen arbitrarily" you mean "chosen randomly" then simply replace the ORDER BY whatever in ypercube's query with a call to a function to randomize the results.
–
Leigh RiffelSep 14 '12 at 17:05

1

I think the version with min() (or max()) is the most straighforward solution (and possibly the fastest as well)
–
a_horse_with_no_nameSep 14 '12 at 19:53

@LeighRiffel It need not be random, any choice, as simple as the first one encountered works fine.
–
WilliamKFSep 14 '12 at 20:20