SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sql Query for 4 tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ravi.talamalla
Starting Member

India
2 Posts

Posted - 10/10/2012 :  01:29:50  Show Profile  Reply with Quote
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

Edited by - ravi.talamalla on 10/10/2012 04:39:24

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 10/10/2012 :  02:01:22  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/10/2012 :  07:33:11  Show Profile  Reply with Quote
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

India
2 Posts

Posted - 10/10/2012 :  08:37:20  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 10/11/2012 :  00:11:51  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000