I have a sql view, call it SampleView
, the results of which have the following format.
Id (INT), NameA (VARVHAR(50)), NameB (VARCHAR(50)), ValueA (INT), ValueB (INT)
The result set of the view contains strings that can have the same Id
or not. When there are two or more lines with the same identifier, I would like to get something like the following
SELECT Id, MAX(NameA), MAX(NameB), MAX(ValueA), MAX(ValueB) FROM SampleView GROUP BY Id ORDER BY Id
As for the columns Id
, ValueA
and ValueB
, then no problems arise. On the other hand, using MAX
for NameA
and NameB
, things are not going as expected. After some searching and searching, I realized that MAX
does not have the βexpectedβ behavior for alphanumeric columns. Speaking of the expected, I mean using MAX
in my case, this would have to return the NameA
value with the maximum number of characters, MAX(LEN(NameA))
. I should mention here that it is not possible for NameA
have two values ββfor the same Id
with the same length. This can make the task easier.
I am using SQL Server 2012 and TSQL
.
Do you have any suggestion on how I can deal with this problem?
Thanks in advance for any help.
source share