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 |
Erik.doe
Starting Member
6 Posts |
Posted - 2014-02-04 : 13:38:01
|
I need to get the result of all fields but only one of each different Field1. It must be the record with the highest Field2 belonging to the same record.TIABest regardsErikTableID Field1 Field2 Field31 AAA 12 ab2 DDD 42 df3 CCC 11 tr4 BBB 15 ik5 DDD 31 mn6 BBB 11 pi7 AAA 16 er8 EEE 56 sw9 BBB 18 dcResult should be in this order:7 AAA 16 er9 BBB 18 dc3 CCC 11 tr2 DDD 42 df8 EEE 56 sw |
|
MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2014-02-04 : 14:12:08
|
[code]WITH CTE AS(select Field1, max(Field2) as mField2 from @YourTable group by Field1)select a.ID, a.Field1, a.Field2, a.Field3 from @YourTable a join CTE c on a.Field2 = c.mField2;[/code] |
|
|
Erik.doe
Starting Member
6 Posts |
Posted - 2014-02-06 : 02:55:46
|
Hi MuMu88It gives me not the correct result.Row with ID 6 shows up in the result. It may not. |
|
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2014-02-06 : 03:16:54
|
;with cte(field2,Field1)as(select MAX(field2),Field1 from @YOURTABLE group by Field1) select v.Id,c.Field1,c.Field2,v.Field3 FROM CTE as cinner join @YOURTABLE as von v.Field2 = c.Field2and v.Field1 = c.Field1ORDER BY c.Field1Veera |
|
|
Erik.doe
Starting Member
6 Posts |
Posted - 2014-02-06 : 03:26:59
|
Veera, Thank You very much.Exactly what I was looking for.Best regardsErik |
|
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2014-02-06 : 03:29:43
|
WelcomeVeera |
|
|
|
|
|