Selecting one row from a unique FK value

This sounds simple, and probably is but having a hard time figuring it out with an elegant solution… basically I’m looking to retrieve a single row based on the foreign key value.

So if we had a table that looks like the following:

id | description | fkid
1  | hello       | 1 
2  | world       | 1
3  | foo         | 2
4  | bar         | 2
5  | this        | 3
6  | that        | 3

It would be good to have the query to return any one id (don’t mind which one) with a unique fk value, something like this:

id | description | fkid
1  | hello       | 1 
3  | foo         | 2
5  | this        | 3

Thanks in advance for any help!

SELECT min(id), description
FROM table
GROUP BY fkid;

Since you didn’t say which criteria you want, I simply chose to apply a function to get the first record (and therefore, the one with the lowest id) for each fkid

molona, your query will return an id and a description for each FK… but the description may not necessarily belong to the id!!!

neil, if this matters, then you have to do something else

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.