SQL Select - Getting a column from a nested select

I just need to get a column from table "Table2-L1-4" in the nested select.  

SELECT  [Table1-FInal].[OrderID]
FROM [Table1-FInal]
(SELECT [Table2-L1-4].[D-ID] as 'Order Detail  ID' FROM [Table2-L1-4]
WHERE [Table1-FInal].[L3 NUM]
      = [Table2-L1-4].[D-Comp] AND [Table2-L1-4].[D-Object] = 'Level 3')

Expected results

Order ID       Order Detail  ID
..........              ........................
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
If you give us the table schema for both tables, it is easier to comment. No idea which keys in the tables are primary and foreign

Try this: (not tested)
SELECT  [Table1-FInal].[OrderID]
FROM [Table1-FInal]
		SELECT [Table2-L1-4].[D-ID] as 'Order Detail  ID' 
		FROM [Table2-L1-4] JOIN [Table1-FInal]
		WHERE [Table1-FInal].[L3 NUM]=[Table2-L1-4].[D-Comp] AND [Table2-L1-4].[D-Object] = 'Level 3'

Open in new window

tmajor99Author Commented:
It should not make a difference.  I am not looking for optimal performance.  Just want to get a column in the next query.  If the record exists, then get me the [Table2-L1-4].[D-ID] returned.
dsackerContract ERP Admin/ConsultantCommented:
Rather than a WHERE EXISTS, simply do an INNER JOIN, which will force the WHERE EXISTS anyway, plus give you anything you want out of the [Table2-L1-4] table.
SELECT  [Table1-FInal].[OrderID],
FROM    [Table1-FInal]
INNER JOIN [Table2-L1-4]
      ON   [Table2-L1-4].[D-Comp] = [Table1-FInal].[L3 NUM]
      AND  [Table2-L1-4].[D-Object] = 'Level 3'

Open in new window

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
tmajor99, do you still need help with this question?
Vitor MontalvãoMSSQL Senior EngineerCommented:
So, with this information I think dsacker solution should work. If you want to return NULL if no values has found then change the INNER JOIN to LEFT JOIN:
SELECT  [Table1-FInal].[OrderID], [Table2-L1-4].[D-ID]
FROM    [Table1-FInal]
LEFT JOIN [Table2-L1-4] ON   [Table2-L1-4].[D-Comp] = [Table1-FInal].[L3 NUM]  AND  [Table2-L1-4].[D-Object] = 'Level 3'

Open in new window

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.