Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sql Query for 4 tables

Author  Topic 

ravi.talamalla
Starting Member

2 Posts

Posted - 2012-10-10 : 01:29:50
Hi,

I have 4 tables. all tables having common column(MapId).
MapId is primary key of one Master Table and Foreign key of below tables.how can i fetch the records from all table passing Mapid in where condition

ex:-

Table1 Table2 Table3 Table4
Id Id Id Id
MapId MapId MapId MapId
FirstName SecondName SurName FatherName



OutPut



FirstName SecondName SurName FatherName MapId
- - - - -
- - - - -



example for out put data




FirstName SecondName SurName FatherName MapId
ravi siva madala chenchai 500
krish nag kondra swamy 500
satish chitu talamalla sample 500
babu peci podili parlapa 500

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-10 : 02:01:22
show some sample data and explain what you need as output. what according to you're duplicates?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-10 : 07:33:11
The description you have "MapId is primary key of one Master Table and Foreign key of below tables" and the sample data where a number of rows have MapId=500 makes it hard to figure out which FirstName associates with which second name etc.

You might try this to see if that is what you are looking for, but I suspect not:
SELECT
t1.FirstName,
t2.SecondName,
t3.SurName,
t4.FatherName,
t1.MapId
FROM
Table1 t1
INNER JOIN Table2 t2 ON t2.Id = t1.Id
INNER JOIN Table3 t3 ON t3.Id = t1.Id
INNER JOIN Table4 t4 ON t4.Id = t1.Id
Go to Top of Page

ravi.talamalla
Starting Member

2 Posts

Posted - 2012-10-10 : 08:37:20
Thanks for your query. But i am getting output as cross join.
quote:
Originally posted by sunitabeck

The description you have "MapId is primary key of one Master Table and Foreign key of below tables" and the sample data where a number of rows have MapId=500 makes it hard to figure out which FirstName associates with which second name etc.

You might try this to see if that is what you are looking for, but I suspect not:
SELECT
t1.FirstName,
t2.SecondName,
t3.SurName,
t4.FatherName,
t1.MapId
FROM
Table1 t1
INNER JOIN Table2 t2 ON t2.Id = t1.Id
INNER JOIN Table3 t3 ON t3.Id = t1.Id
INNER JOIN Table4 t4 ON t4.Id = t1.Id




raveendra
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-11 : 00:11:51
quote:
Originally posted by ravi.talamalla

Thanks for your query. But i am getting output as cross join.
quote:
Originally posted by sunitabeck

The description you have "MapId is primary key of one Master Table and Foreign key of below tables" and the sample data where a number of rows have MapId=500 makes it hard to figure out which FirstName associates with which second name etc.

You might try this to see if that is what you are looking for, but I suspect not:
SELECT
t1.FirstName,
t2.SecondName,
t3.SurName,
t4.FatherName,
t1.MapId
FROM
Table1 t1
INNER JOIN Table2 t2 ON t2.Id = t1.Id
INNER JOIN Table3 t3 ON t3.Id = t1.Id
INNER JOIN Table4 t4 ON t4.Id = t1.Id




raveendra


it wont give you cross join anyways but i guess whats happening is you're getting lot more than required because of one to many relationships existing in between.
so you need to explain us with some sample data what exact output you're expecting out of them and the rule for choosing them

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -