I have a table with millions of rows of data in SQL Server 2008. I am trying to find an alternative way instead of using distinct. Please see the query below:
create table #temp (id int)
create table #temp2 (id int, name varchar(55), t_id int)
insert into #temp values (1)
insert into #temp2 values (1,'john',1)
insert into #temp2 values (2,'alex',1)
insert into #temp2 values (3,'alex',1)
select t.id, t2.name
from #temp t
inner join #temp2 t2 on t.id = t2.t_id
This query returns output like:
Id Name
1 john
1 alex
1 alex
The expected output is:
Id Name
1 john
1 alex
I can provide the expected output by using DISTINCT
keyword, I know it, but it decreases the performance. Could you please advise me some professional alternative ways (except using group by) to handle it? Thanks!
Edit: I have a custom concentenate function which helps me to do:
select t.id, concetenate(t2.name)
from #temp t
inner join #temp2 t2 on t.id = t2.t_id
and this is returning 1 john,alex,alex
. I am looking for a way to get rid of one of the alex
without updating the function and do not want to use "distinct
" keyword.