Question

Possible Duplicate:
Oracle SQL - How to Retrieve highest 5 values of a column

I'm writing oracle query but stuck in the following problem

table is like this:

**Tool**       **Name**       **Gender**
Facebook   Alice        F
Facebook   Alex         M
Facebook   Loong        M
Facebook   Jimmy        M
Twitter    James        M
Twitter    Jessica      F
Twitter    Sam          M
Twitter    Kathrine     F
Google     Rosa         F
Google     Lily         F
Google     Bob          M

What I wanna get is the first female in each tool the result should be like:

Facebook   Alice
Twitter    Jessica
Google     Rosa

I'm trying to get this by using query not functions or procedures Thank for helping

Was it helpful?

Solution

select  *
from    (
        select  row_number() over (partition by tool order by name) as rn
        ,       Name
        ,       Tool
        from    YourTable
        where   Gender = 'F'
        ) SubQueryAlias
where   rn = 1 -- Only first per tool

Example at SQL Fiddle.

OTHER TIPS

This is another alternative.

select min(name), tool
from yourTable
where gender = 'F'
group by tool

I'd like to have a little bit of a discussion on which is better or which does what, for me its the first time I see row_number(). Note thas this one returns the female in the alphabetical order, yours does the same by sorting in a window, what is the difference?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top