hojohappy
asked on
SQL Nested Select
I need to display a column from a nested selected query.
SELECT ProductID
FROM [USMapTemplate]
where ([USMapTemplate].[ProductI D] 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
SELECT ProductID
FROM [USMapTemplate]
where ([USMapTemplate].[ProductI
(Select [Master].[ProductName] from [Attribute Master] where CHARINDEX([USMapTemplate].
My output needs to include the Product Name column from the nested table [Master] table.
ProductID Product Name
-------------- ---------------------
ddddddd dhdhhdhdhh ddddddddd
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This probably will be slow, but I think this will work.
[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
SELECT
mt.ProductID
, am.ProductName
FROM USMapTemplate mt
INNER JOIN [Attribute Master] am
ON CHARINDEX(mt.ProductID, am.ProductName) > 1
;
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
)
;
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
try this
SELECT ProductID, ProductName
FROM [USMapTemplate]
where ([USMapTemplate].[ProductI D] in
(Select [Master].[ProductName] from [Attribute Master] where CHARINDEX([USMapTemplate]. [ProductID ], [Master].[ProuctName]) > 1))
SELECT ProductID, ProductName
FROM [USMapTemplate]
where ([USMapTemplate].[ProductI
(Select [Master].[ProductName] from [Attribute Master] where CHARINDEX([USMapTemplate].
with cte as
(select productname
from [Attribute Master])
select mt.productid, cte.productname
from usmaptemplate, cte
where charindex(mt.productid,cte .productna me) > 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.
(select productname
from [Attribute Master])
select mt.productid, cte.productname
from usmaptemplate, cte
where charindex(mt.productid,cte
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.
Open in new window