Solved

SQL Server Query

Posted on 2012-03-21
12
253 Views
Last Modified: 2012-06-21
I am trying to write a query on SQL Server to display results from 3 tables, called:
Table1, Table2 and Table3
WHERE Table1.ColumnA='x' and Table2.ColumnB='y' and Table3.columnC='z'
Any ideas?   Thanks.
0
Comment
Question by:fjkaykr11
  • 6
  • 5
12 Comments
 
LVL 18

Expert Comment

by:sventhan
ID: 37749062
do you have a common column to join between these 3 tables?
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 37749262
Yes there is an ID column listed in all 3 tables.
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37749340
select t2.id, t2.loginname,t2.wrknum,t1.time,t3.description
from table2 t2
inner join table1 t1 on t1.id=t2.id
inner join table3 t3 on t3.id=t2.id
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 3

Author Comment

by:fjkaykr11
ID: 37749385
Thanks for the reply. I will give this a try.
0
 
LVL 18

Expert Comment

by:sventhan
ID: 37749426
see if this works ..

select t1.ColumnA,t2.ColumnB,t3.ColumnC
from table1 t1,table2 t2,table3 t3
inner join table1 t1 on t1.id=t2.id
inner join table3 t3 on t3.id=t2.id
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 37749461
@ashok111.  I just realized this won't work. I need to filter for specific criteria in each column with the where clause (as posted in my original question).  
WHERE Table1.ColumnA='x' and Table2.ColumnB='y' and Table3.columnC='z'
Please advise how can I do this.
0
 
LVL 18

Expert Comment

by:sventhan
ID: 37749521
just add the filter as below...

select t1.ColumnA,t2.ColumnB,t3.ColumnC
from table1 t1,table2 t2,table3 t3
inner join table1 t1 on t1.id=t2.id
inner join table3 t3 on t3.id=t2.id
WHERE Table1.ColumnA='x' and Table2.ColumnB='y' and Table3.columnC='z'
0
 
LVL 18

Expert Comment

by:sventhan
ID: 37749523
it should be
select t1.ColumnA,t2.ColumnB,t3.ColumnC
from table1 t1,table2 t2,table3 t3
inner join table1 t1 on t1.id=t2.id
inner join table3 t3 on t3.id=t2.id
WHERE T1.ColumnA='x' and T2.ColumnB='y' and T3.columnC='z'
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 37749692
Is there way to write this without doing an alias name? I keep getting multiple syntax errors, no matter what I change I can't getting it when trying to follow the syntax logic above.
errors include:
The correlation name 'T1' has the same exposed name as table 'T1'.
or
The Table name T1 is specified multiple times in the FROM Clause
or
Msg 107, Level 16, State 2, Line 1
The column prefix 'T1' does not match with a table name or alias name used in the query.
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 37749742
When I remove all references to the alias names, I get the error:
Msg 107, Level 16, State 2, Line 1
The column prefix 'Table1' does not match with a table name or alias name used in the query.
0
 
LVL 18

Accepted Solution

by:
sventhan earned 500 total points
ID: 37749850
sorry my bad

try now...

select t1.ColumnA,t2.ColumnB,t3.ColumnC
from table1 t1,table2 t2,table3 t3
where t1.id = t2.id
and t2.id = t3.id
and T1.ColumnA='x'
and T2.ColumnB='y'
and T3.columnC='z'
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 37750667
@sventhan that worked!  Thanks so much for following up.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 Make faster Temp Table 17 103
Trying to get a Linked Server to Oracle DB working 21 57
Webservices in T-SQL 3 30
Access 2003 query lost it's only join 7 26
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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

808 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