I have a sql query with several joins and then saves it in a temporary table, well summarized it is something like this:
SELECT
a.Id,
a.Nome,
b.Telefone,
c.Email
INTO #TEMP
FROM TabelaA A
INNER JOIN TabelaB B ON ...
LEFT JOIN TabelaC C ON ...
Then a subquery with dynamic sql is used to make different queries based on this data from the temporary table, something like this:
SET @query = 'WITH SUBQUERY (
SELECT DISTINCT *
FROM #TEMP
GROUP BY
Id,
Nome,
Telefone,
Email
)'
SET @query = @query + 'SELECT DISTINCT ...'
My question is, does invoking DISTINCT over and over on the same subset make any difference or is it just doing it once?