SQL Nested Select

I need to display a column from a nested selected query.

SELECT ProductID
FROM [USMapTemplate]
where ([USMapTemplate].[ProductID] EXISTS
(Select [Master].[ProductName] from [Attribute Master] where CHARINDEX([USMapTemplate].[ProductID], [Master].[ProuctName]) > 1)

My output needs to include the Product Name column from the nested table [Master] table.  

ProductID     Product Name
--------------     ---------------------
ddddddd      dhdhhdhdhh ddddddddd
hojohappyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy Knight, MCMPrincipal ConsultantCommented:
You shouldn't need a subquery for this.   Something like this should work.

SELECT a.ProductID, b.ProductName
FROM USMapTemplate a
	LEFT JOIN AtttributeMaster b ON a.ProductID = b.ProductID AND CHARINDEX(a.ProductID, b.ProductName) > 1
WHERE b.ProductID IS NOT NULL

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chaauCommented:
You can convert your query to INNER JOIN:
SELECT [USMapTemplate].ProductID, [Attribute Master].[ProductName] 
FROM [USMapTemplate] INNER JOIN [Attribute Master] 
     ON CHARINDEX([USMapTemplate].[ProductID], [Attribute Master].[ProductName]) > 1

Open in new window

0
PortletPaulfreelancerCommented:
This probably will be slow, but I think this will work.
SELECT
      mt.ProductID
    , am.ProductName
FROM USMapTemplate mt
      INNER JOIN [Attribute Master] am
                  ON CHARINDEX(mt.ProductID, am.ProductName) > 1
;

Open in new window

By the way, the query code you posted in the question has syntax errors it should look like this:
SELECT
      mt.ProductID
FROM USMapTemplate mt
WHERE EXISTS (
            SELECT
                  NULL
            FROM [Attribute Master] AS am
            WHERE CHARINDEX(mt.ProductID, am.ProductName) > 1
      )
;

Open in new window

Notes:
[Attribute Master] I don't know if this is a table name (boy I hope not) or if the alias was intended to be "Master"
I wouldn't recommend using "Master" as an alias (could be confused with the master db)

ProuctName I assume is ProductName
0
Pratik MakwanaData AnalystCommented:
try this

SELECT ProductID, ProductName
FROM [USMapTemplate]
where ([USMapTemplate].[ProductID] in
(Select [Master].[ProductName] from [Attribute Master] where CHARINDEX([USMapTemplate].[ProductID], [Master].[ProuctName]) > 1))
0
awking00Commented:
with cte as
(select productname
 from [Attribute Master])
select mt.productid, cte.productname
from usmaptemplate, cte
where charindex(mt.productid,cte.productname) > 0

Note - where charindex > 0, using where charindex > 1 would not return a record where the productid was 'eeeeee' and the productname was 'eeeeeeee efefefefef', for example.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.