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 |
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2008-08-21 : 11:38:42
|
| intTransactionID intAliasID, Enrollment8166 17854 38121 17850 48142 17850 48223 17827 28149 17811 58128 17811 5My data looks like the above. What I need to do is grab the intTransactionID of the intAliasID, but only the greatest one for the pair. So, in the highlighted area above, what I really want to return isintTransactionID intAliasID, Enrollment8166 17854 38142 17850 48223 17827 28149 17811 5I basically want to strip out the dupe. Any thoughts? Thanks |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2008-08-21 : 11:40:41
|
| SELECT TOP 1 |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-08-21 : 11:53:50
|
| [code]create table #mytable (intTransactionID int, intAliasID int, Enrollment int)insert into #mytableselect 8166 ,17854 ,3 union allselect 8121, 17850, 4 union allselect 8142, 17850, 4 union allselect 8223, 17827, 2 union allselect 8149, 17811, 5 union allselect 8128, 17811, 5 select a.* from #mytable a left join #mytable b on a.intAliasID = b.intAliasID and a.intTransactionID < b.intTransactionIDwhere b.intTransactionID is nulldrop table #mytable [/code]"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2008-08-21 : 11:54:29
|
| If I use select TOP 1, I will obviously only get one row back...I need to get all the rows back, just no dups for the given intAliasID.... |
 |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2008-08-21 : 11:59:49
|
| SELECT DISTINCT |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-08-21 : 12:22:36
|
Jhocutt's method works, there is also the 2005 way (this will also handle N number of Alias IDs that are the same, not just 2):DECLARE @Yak TABLE (intTransactionID int, intAliasID int, Enrollment int)insert into @Yakselect 8166 ,17854 ,3 union allselect 8121, 17850, 4 union allselect 8142, 17850, 4 union allselect 8223, 17827, 2 union allselect 8149, 17811, 5 union allselect 8128, 17811, 5 SELECT intTransactionID, intAliasID, Enrollment FROM ( SELECT *, ROW_NUMBER() OVER(PARTITION BY intAliasID ORDER BY intTransactionID DESC) AS RowNum FROM @Yak ) AS TempWHERE RowNum = 1 |
 |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2008-08-21 : 13:04:57
|
| Thanks folks, both methods worked out. I appreciate the help.HC |
 |
|
|
|
|
|
|
|