Solved

i need a tsql query

Posted on 2013-05-23
2
159 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now