Look at this SQL code written by Linoff:
approach used by Taryn for SQL concatenation
SELECT a.*,
(SELECT STUFF( (SELECT DISTINCT '; ' + r.[description]
FROM [tdt_AD_Teste] r
WHERE a.pid = r.pid AND
r.[displayname] = 'member'
FOR XML PATH('')
), 1, 2, ''
)
) Descript
FROM (SELECT a.pid,
MAX(CASE WHEN a.displayname = 'name' then [group] end) as name,
MAX(CASE WHEN a.displayname = 'sd' then [group] end) as sd,
MAX(CASE WHEN a.displayname = 'description' then [group] end) as description
FROM tdt_AD_Teste a
GROUP BY a.pid
) a;
This code will generate 5 columns: pid ; name; sd; description; descript
I want to make a search for the Descript part (it is an alias)...
if we write as:
where [Descript] like '%' + '<any_value>'+ '%'
it will throw an invalid column name 'Descript' (looking for pid, sd, description and name is easy).
If I write the same code as you see above... WITH A WRAP select * from (code) b where (...)
SELECT *
FROM
(
SELECT a.*,
(SELECT STUFF( (SELECT DISTINCT '; ' + r.[description]
FROM [tdt_AD_Teste] r
WHERE a.pid = r.pid AND
r.[displayname] = 'member'
FOR XML PATH('')
), 1, 2, ''
)
) Descript
FROM (SELECT a.pid,
MAX(CASE WHEN a.displayname = 'name' then [group] end) as name,
MAX(CASE WHEN a.displayname = 'sd' then [group] end) as sd,
MAX(CASE WHEN a.displayname = 'description' then [group] end) as description
FROM tdt_AD_Teste a
GROUP BY a.pid
) a ) b
WHERE b.Descript like '%' + '<any_value> + '%' ;
it works but it takes too much time to find some string.... (it can take around20 s...). Is there a faster way to do the search for this case?