Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

i need a tsql query

Posted on 2013-05-23
2
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 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 49

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 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