Solved

i need a tsql query

Posted on 2013-05-23
2
161 Views
Last Modified: 2013-06-09
Hi, i need a query which select a value from a join (join1) and uses this value as input for a second join (join2), which is the endresult.

Join1 contains only one row, because a selection is made on a column which is key:-

SELECT     tableb.code2 as result1 FROM tablea INNER JOIN tableb
                      ON table1.code1 = tableb.code1
WHERE     (tablea.key = 12105880)

Join 2

SELECT     .... FROM tablea INNER JOIN tableb
                      ON table1.code1 = tableb.code1
WHERE     (tableb.code2 = result1)

can this be combined in one query?

thanks
0
Comment
Question by:aatjan
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 39191731
SELECT     .... FROM tablea INNER JOIN tableb
                      ON table1.code1 = tableb.code1
WHERE    tableb.code2 IN (SELECT     tableb.code2 as result1 FROM tablea INNER JOIN tableb
                      ON table1.code1 = tableb.code1
WHERE     (tablea.key = 12105880)
)
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39193304
alternatively leverage the inner join
SELECT 
  .... 
FROM tablea 
INNER JOIN tableb ON table1.code1 = tableb.code1
AND  tableb.code2 =  (
                        SELECT tableb.code2
                        FROM tablea 
                        INNER JOIN tableb ON table1.code1 = tableb.code1
                        WHERE (tablea.key = 12105880)
                      )

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL - Rotating Values in SQL 9 65
Where to download and how to install sqldmo.dll 5 82
Sql query 107 60
SSAS Hierarchy with columns with folder names 10 11
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question