Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 557573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:02:25+00:00 2026-05-13T12:02:25+00:00

Just to head off the quick-repliers, I’ll mention that I’m not using MySQL so

  • 0

Just to head off the quick-repliers, I’ll mention that I’m not using MySQL so I can’t use GROUP_CONCAT, and that I’ve also looked at this question already.

The example
(this is not the data i’m working with but it accurately describes my case)
Person table, with ID, Name columns
Person_CountriesVisited table, with PersonID, Country columns (1..M linking table)
Person_StatesVisited table, with PersonID, State columns (1..M linking table)

The constraints
I can’t modify the DB. I can’t modify much of the code, either, so this has to be done in SQL, not in code after running the query.

The desired result
A result set with the columns

Person.ID,
Person.Name,
[list of related Person_CountriesVisited.Country],
[list of related Person_StatesVisited.State],

which can also be filtered by a generic search term which applies across all columns.

My attempts

Now, my SQL-fu isn’t all that sharp nowadays, but I’m doing what I can based on examples I’ve found.

1) I’ve tried using a scalar sub-query:

SELECT P.ID, P.Name,
( select
CV.Country + ‘, ‘
FROM
Person_CountriesVisited CV ON P.ID =
CV.PersonId
FOR XML PATH (”)) AS
CountriesVisited,
( SELECT
SV.State + ‘, ‘
FROM
Person_StatesVisited SV ON P.ID =
SV.PersonId
FOR XML PATH (”)) AS StatesVisited

FROM Person P

WHERE P.Name LIKE
‘%’ + @SearchTerm + ‘%’
OR
CountriesVisited LIKE ‘%’ +
@SearchTerm + ‘%’
OR StatesVisted
LIKE ‘%’ + @SearchTerm + ‘%’

Which returns the data I want, but only when I remove the WHERE clause. It doesn’t work with the WHERE clause because SQL Server 2005 doesn’t seem to like that i’m referencing a column created from a scalar subquery in the WHERE clause. It’s giving me an “Invalid column name ‘CountriesVisited'” error (and again for the StatesVisited column).

2) I’ve tried the cross apply trick:

SELECT P.ID, P.Name, CountriesVisited,
StatesVisited

FROM Person P
INNER JOIN Person_CountriesVisited CV ON
P.ID = CV.PersonID
CROSS APPLY
(
SELECT CV.Country + ‘, ‘
FROM Person P2 INNER JOIN
Person_CountriesVisited CV ON P2.ID =
CV.PersonID
WHERE P.ID =
P2.ID
FOR XML PATH (”)
)
PRE_TRIMMED (CountriesVisited)

INNER JOIN Person_StatesVisited SV ON
P.ID = SV.PersonID
CROSS APPLY (
SELECT SV.State +
‘, ‘
FROM Person P2 INNER JOIN
Person_StatesVisited SV ON P2.ID =
SV.PersonID FOR XML PATH (”)
)
PRE_TRIMMED (StatesVisited)

WHERE
P.Name LIKE ‘%’ + @SearchTerm +
‘%’
OR CountriesVisited LIKE ‘%’
+ @SearchTerm + ‘%’
OR StatesVisted LIKE ‘%’ + @SearchTerm +
‘%’

But that doesn’t work either since you apparently can’t have multiple pre_trimmed calls.

Anybody have suggestions for me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:02:26+00:00Added an answer on May 13, 2026 at 12:02 pm

    You just need to alias your subqueries:

    WITH x AS (
      SELECT P.ID
            ,P.Name
            ,( select CV.Country + ', '
             FROM Person_CountriesVisited CV ON P.ID = CV.PersonId 
             FOR XML PATH ('')
            ) AS CountriesVisited
            ,( SELECT SV.State + ', '
             FROM Person_StatesVisited SV ON P.ID = SV.PersonId 
             FOR XML PATH ('')
            ) AS StatesVisited
      FROM Person P
    )
    SELECT *
    FROM X    
    WHERE Name LIKE '%' + @SearchTerm + '%'
      OR CountriesVisited LIKE '%' + @SearchTerm + '%'
      OR StatesVisted LIKE '%' + @SearchTerm + '%'
    

    You can do something similar in the second example. Actually in the second example, just alias the second subquery as something new, like PRE_TRIMMED2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I noticed that you can place the <map> tags anywhere in the
Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.
Just trying to get my head around Generics by reading this enlightening article by
Just trying to get my head round Spring and figuring out how I wire
Just looking for the first step basic solution here that keeps the honest people
just a quick question: I am a CS undergrad and have only had experience
Just how much slower are events? I have written a streaming XML parser (that
The HTTP/1.1 RFC stipulates The HEAD method is identical to GET except that the
Just getting into C++. I'm getting constantly thrown off track when I see the
You can actually just skip this first part cause it gets confusing, but I'll

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.