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 |
|
Davide70
Starting Member
6 Posts |
Posted - 2012-02-16 : 06:21:34
|
| Hello to all experts,we have the following table:T(id, begin, end, team, men, women, rank)Now we want to get men , women and rank for team xxwhere end is the maximum value in the table for that team.We tried the following query:SELECT MAX(end), men, women, rankFROM Twhere team=xxGROUP BY men, women, rankbut we get more rows then expected, not just the one corresponding to max end (in this case 2 rows for the same rank):End____Men____Women___Rank862194__2_______0_______41892037_1_______0_______41302560_3_______0______141450814_2_______0______14same problem using having end=max(end). Suggestions?Thank you very much,Davide |
|
|
rajan.nithin
Starting Member
42 Posts |
Posted - 2012-02-16 : 07:32:50
|
| Try This:SELECT MAX(end), men, women, rankFROM Twhere team=xxGROUP BY team,men, women, rank |
 |
|
|
Davide70
Starting Member
6 Posts |
Posted - 2012-02-16 : 09:01:08
|
Thank you,but the result is the same..quote: Originally posted by rajan.nithin Try This:SELECT MAX(end), men, women, rankFROM Twhere team=xxGROUP BY team,men, women, rank
|
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-02-16 : 09:06:26
|
What should be the output for the example you showed?You may be able use something like what I am showing below, or may be you need to use a summation on men, women etc. In the query below I have included only team in the PARTITION clause - you may need to add other columns depending on your requirement.SELECT END, men, women, RANKFROM( SELECT End men, women, RANK, ROW_NUMBER() OVER (PARTITION BY team ORDER BY END DESC) AS RN FROM T WHERE team = xx) sWHERE RN = 1 |
 |
|
|
Davide70
Starting Member
6 Posts |
Posted - 2012-02-16 : 10:27:51
|
Only the 2nd and 4th rows, those with max End..Thank you,quote: Originally posted by sunitabeck What should be the output for the example you showed?You may be able use something like what I am showing below, or may be you need to use a summation on men, women etc. In the query below I have included only team in the PARTITION clause - you may need to add other columns depending on your requirement.SELECT END, men, women, RANKFROM( SELECT End men, women, RANK, ROW_NUMBER() OVER (PARTITION BY team ORDER BY END DESC) AS RN FROM T WHERE team = xx) sWHERE RN = 1
|
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-02-16 : 12:48:38
|
Change the partition by clause to include rank alsoROW_NUMBER() OVER (PARTITION BY team, rank ORDER BY END DESC) AS RN |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|
|
|