Search code examples
sqloracle-databasetop-n

how to get first female of each tool, oracle database


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


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.