I am using SQL Server 2012. I was trying to resolve a "typical" grouping problem. But I couldn't resolve this correctly.
Imagine that you have this table:
I would like to group by position. It could be easy:
select position, COUNT(*)
from testsql
group by position
But, I would like to group by position, and take the longest name (myname) of every position.
select
myid, myname, position, len(myname) as lenname
from
testsql
group by
myid, myname, position
having
myname = select (max(len(myname))) from testsql /*Obviously it doesn't work */
order by
position asc;