Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
IainT
Starting Member
12 Posts |
Posted - 2007-04-20 : 05:40:40
|
| Hey guysI'm extremely puzzled by some SQL and in need of some help.The table set up is very simple - I have just one table called Gift. It has three relevant fields - giftID, giftDate and constituentID. I need to find the earliest giftID for each constituentID. Simple, ya?So if the data wasgiftID.....giftDate.....constituentID3..........1/1/2006..........21..........1/1/2005..........25..........2/2/2006..........35..........2/2/2006..........28..........8/8/2006..........39..........1/1/2005..........2It should return:giftID....constituentID1...........25...........3or the following would also be fine:giftID....constituentID9...........25...........3It should not return:giftID....constituentID1...........25...........39...........2Any help would be gratefully received! Any ideas?CheersIain |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-20 : 06:22:13
|
| Why not both 1 and 9? How SQL is going to select either of one when dates for both are same?Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-20 : 06:25:32
|
| [code]SELECT GiftID, GiftDate, ConstituentIDFROM ( SELECT GiftID, GiftDate, ConstituentID, ROW_NUMBER() OVER (PARTITION BY ConstituentID ORDER BY GiftDate) AS RecID FROM Table1 ) AS dWHERE RecID = 1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
IainT
Starting Member
12 Posts |
Posted - 2007-04-20 : 06:25:34
|
| harsh_athalye: Because I only want one gift per constituent, but if both are on the same date then it doesn't matter which one is selected. It's an unusual situation.Peso: Thanks. I'll have a look at that and get back to you. |
 |
|
|
|
|
|