About Me

My photo
MCTS FOR SQL SERVER 2008 DOT NET PROGRAMMING WEB DESIGNING

Saturday, 5 May 2012

using table twice in select stmt

i've been asked a question like this:-

table structure is:

mytableid int pk
userid int
counterid int


in this table he has some data like this:

MyTableID   UserID   Counter
---------   -------  --------
1           1         4
2           1         7
3           4         3
4           11        9
5           11        3
6           4         6
...
9           11        9


now his query is, he wants use a select stmt which gives the result with max counterid and the userid should not repeat.......

so many peoples are posted so many syntaxes.........but he is not satisfied with their syntaxes.....

then...i gave the solution like this

select distinct(a.userid),
MAX(a.counterid) as counterid
 from mytable a, mytable b
where a.mytableid=b.mytableid group by a.userid





ff
f
finally it works............


these all are done in famous site WWW.STACKOVERFLOW.COM

No comments:

Post a Comment