tsql group by get alphanumeric column value with maximum length

0

Please login or register to vote for this query.

(click on this box to dismiss)

Engineering

Q&A for professionals and students of engineering

create table #SampleView (id int, NameA varchar(10), ValueA int,
                         NameB varchar(10), ValueB int)

insert into #SampleView values
(1, '123', 10, '234', 23),
(1, '1233', 3, '23334', 33),
(1, '122223', 8, '2334', 55),
(2, '123', 5, '234', 45),
(2, '1423', 47, '43234', 102)
 
 SELECT DISTINCT 
       id,
       FIRST_VALUE(NameA) OVER (PARTITION BY id ORDER BY len(NameA) DESC),
       MAX(ValueA) OVER (PARTITION BY id),
       FIRST_VALUE(NameB) OVER (PARTITION BY id ORDER BY len(NameA) DESC),
       MAX(ValueB) OVER (PARTITION BY id)       
FROM #SampleView

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results