Solved

Selection from table2 where criteria for table1

Posted on 2016-11-07
10
77 Views
Last Modified: 2016-11-07
Hello,

I need to select all refno in table2 where column1 from table1 ='yes'



table1:
Ref --Column1
Auth --Column2

table2:
Ref --Column1
Auth --Column2


Select * from table2 where table1 . auth ='yes'

Any suggestions?

Cheers
0
Comment
Question by:RIAS
[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
  • 5
  • 3
  • 2
10 Comments
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41876755
Try this.. I have used left join , you can define that based on your requirement.

SELECT * FROM 
table2 b LEFT JOIN table1 a ON a.Column1 = b.Column1
WHERE a.Column1 = 'Yes'

Open in new window


or

SELECT * FROM 
table2 b LEFT JOIN table1 a ON a.Column1 = b.Column1
AND a.Column1 = 'Yes'

Open in new window

0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41876760
I would prefer to use inner join like this way:
select b.Ref
from table2 b
inner join table1 a 
on b.Ref = a.Ref
where a.Auth = 'Yes'
group by b.Ref

Open in new window

0
 

Author Comment

by:RIAS
ID: 41876764
Ryan your query did not return any value
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:RIAS
ID: 41876766
Pawan,
is there any way I can find distinct values from table2 based on the selection.
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41876768
@RIAS,

pls provide sample data so we can provide proper solution
0
 

Author Comment

by:RIAS
ID: 41876770
Pawan's query worked!
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41876771
Use Distinct... Here you can use * for all columns or column name like ref..

Select DISTINCT  *  FROM  
table2 b LEFT JOIN table1 a ON a.Column1 = b.Column1
WHERE a.Column1 = 'Yes'

Select DISTINCT Ref  FROM  
table2 b LEFT JOIN table1 a ON a.Column1 = b.Column1
WHERE a.Column1 = 'Yes'

OR

SELECT DISTINCT * FROM
table2 b LEFT JOIN table1 a ON a.Column1 = b.Column1
AND a.Column1 = 'Yes'
0
 

Author Comment

by:RIAS
ID: 41876774
Select DISTINCt Ref  FROM  
table2 b LEFT JOIN table1 a ON a.Column1 = b.Column1
WHERE a.Column1 = 'Yes'

errors as it does not know which table it belongs to
0
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41876775
Use alias..

Select DISTINCt b.Ref  FROM  
table2 b LEFT JOIN table1 a ON a.Column1 = b.Column1
WHERE a.Column1 = 'Yes'
0
 

Author Closing Comment

by:RIAS
ID: 41876776
Thanks Pawan!
Thanks Ryan as well!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
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…

690 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