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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting one row for each id

Status
Not open for further replies.

SQLJoe

Technical User
Dec 8, 2010
43
US

I need to be able to select only one row out of a table for each donor id present, even when the donor id is listed multiple times (once for each donation).

For example, my data looks like this:

donor1 fund_a amt_a
donor1 fund_b amt_b
donor1 fund_c amt_c
donor2 fund_a amt_a
donor3 fund_c amt_c
donor3 fund_d amt_d

I need my query results to look like this:

donor1 fund_a amt_a
donor2 fund_a amt_a
donor3 fund_c amt_c

(It actually doesn't matter which fund/amt are listed, so long as each donor is listed only once.)

I'm unclear on how to accomplish this. I'm sure there's an easy way, but I've never had a business need like this.


 
The simplest way will be (SQL 2005+):
Code:
;with cte as (select *, row_number() over (partition by Donor
order by Fund, Amt) as Row from DonorsInfo)

select * from cte where Row = 1

PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top