Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Top one for each group by

Status
Not open for further replies.

SQLScholar

Programmer
Aug 21, 2002
2,127
GB
Hey all,

I have this query - which isnt quite doing what i want.

Code:
SELECT  tblPolicyBrokerTeam.TeamName ,
        UserName
FROM    adw.policy.tblAcceptedByUser
        INNER JOIN adw.policy.tblPolicyBrokerTeam
            ON tblAcceptedByUser.BrokerTeamId = tblPolicyBrokerTeam.PolicyBrokerTeamId
ORDER BY UserStatus ,
        tblAcceptedByUser.ADWLoadDate DESC ,
        tblAcceptedByUser.ADWUpdateDate DESC

Basically my problem is i want to bring out the top teamname of this query for each username (ordered by the order by clause).

Now i keep thinking its something to do with RANK by but i cant just put my finger on what i need to do.

Any help much appreaciated.

Dan

----------------------------------------

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch

Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
 
What is "Top team name" ???
How do you define it as TOP?

Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
Sorry for not being clear. As per the order by clause.

Dan

----------------------------------------

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch

Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
 
So effectively with my query if you took the first one of each username you came to, and removed the rest that would be perfect.

Ta

Dan

----------------------------------------

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch

Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
 
Aha..... I need Row_number not rank.

This seems to work:

SELECT *
FROM ( SELECT tblPolicyBrokerTeam.TeamName ,
UserName ,
ROW_NUMBER( ) OVER ( PARTITION BY UserName ORDER BY UserStatus , tblAcceptedByUser.ADWLoadDate DESC , tblAcceptedByUser.ADWUpdateDate DESC ) AS rowNum
FROM adw.policy.tblAcceptedByUser
INNER JOIN adw.policy.tblPolicyBrokerTeam
ON tblAcceptedByUser.BrokerTeamId = tblPolicyBrokerTeam.PolicyBrokerTeamId
) UserDefaultTeam
WHERE rownum = 1





----------------------------------------

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch

Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top