Solved

i need a tsql query

Posted on 2013-05-23
2
164 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 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

630 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