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
 General SQL Server Forums
 New to SQL Server Programming
 Retrieval by combining 3 tables

Author  Topic 

nbalraj
Starting Member

21 Posts

Posted - 2010-08-17 : 00:30:46
User table
-----------
user_id(pk)
first_name
physical_loc
service_type

user affiliated location table
------------------------------
user_id(pk)(fk)
affiliate_loc

case table
-----------
case_id(pk)
user_id(fk)
service_type

I need to display all the users first_name, whose service_type, physical_loc and afiliate_loc are the same as the logged in user.

so the input to the query will be the user_id

can any one give some ideas?

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2010-08-17 : 08:27:58
join the tables using user_id

--------------------
keeping it simple...
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-08-17 : 08:35:32
quote:
Originally posted by nbalraj

User table
-----------
user_id(pk)
first_name
physical_loc
service_type

user affiliated location table
------------------------------
user_id(pk)(fk)
affiliate_loc

case table
-----------
case_id(pk)
user_id(fk)
service_type

I need to display all the users first_name, whose service_type, physical_loc and afiliate_loc are the same as the logged in user.

so the input to the query will be the user_id

can any one give some ideas?


Here's one way
select b.first_name,b.service_type,c.affiliate_loc
from
(select * from User_Table where [user_id] = @user_id) a
inner join User_Table b on a.first_name = b.first_name and a.service_type = b.service_type
inner join User_Affiliated_Location c on a.affiliate_loc = c.affiliate_loc
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-17 : 11:07:26
quote:
Originally posted by vijayisonly

quote:
Originally posted by nbalraj

User table
-----------
user_id(pk)
first_name
physical_loc
service_type

user affiliated location table
------------------------------
user_id(pk)(fk)
affiliate_loc

case table
-----------
case_id(pk)
user_id(fk)
service_type

I need to display all the users first_name, whose service_type, physical_loc and afiliate_loc are the same as the logged in user.

so the input to the query will be the user_id

can any one give some ideas?


Here's one way
select b.first_name,b.service_type,c.affiliate_loc
from
(select * from User_Table where [user_id] = @user_id) a
inner join User_Table b on a.first_name = b.first_name and a.service_type = b.service_type
inner join User_Affiliated_Location c on a.affiliate_loc = c.affiliate_loc



where are you checking for logged in user?

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

Go to Top of Page
   

- Advertisement -