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.
| Author |
Topic |
|
mnolting
Starting Member
36 Posts |
Posted - 2007-06-11 : 19:27:42
|
| I am having trouble building a query that joins three tables. The tables are:Table------------key fields Organizations---org_KEY (all fields start with org_)Locations-------LOC_KEY--LOC_ORG_KEY (all fields start with loc_)Users-----------USR_ORG_KEY----USR_LOC_KEY (all fields start with usr_)Every location record is tied to a record in the organization table. (Via loc_key=org_key)Each user record belongs to one organization and one location. (usr_org_key=org_key and usr_loc_key=loc_key)Not all locations have a user. So if there is no user for any location ,I need the address info.I need a result that is of all organizations, Locations and usersNot all locations have a user but they all have address information eg (loc_address1, loc_city, etc....)The result will look like this:Org_id, org_name, loc_id, loc_name, loc_address1,loc_city,usr_id,usr_namegrouped by organization, then location, then userThanks for all help |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-11 : 23:39:48
|
| post sample data for your 3 tables and required output u want.--------------------------------------------------S.Ahamed |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-06-12 : 00:17:51
|
Something like this:Selecto.Org_id, O.org_name, l.loc_id, l.loc_name, l.loc_address1, l.loc_city, Coalesce(u.usr_id, '') as Usr_id, Coalesce(u.usr_name, '') as usr_nameFrom Organizations o Join Locations l on o.org_KEY = l.org_KEYLeft Join Users u on u.USR_LOC_KEY = o.org_KEYOrder by o.Org_id, l.loc_id, u.usr_id Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
mnolting
Starting Member
36 Posts |
Posted - 2007-06-15 : 22:42:39
|
| I have not tried Harsh's solution (I am not at work right now), however here is a sample of data and outputOrganizationsOrg_key---Org_name1---------Microsoft2---------Digital ResearchLocationsLoc_org_key---Loc_key----Loc_name-------loc_address1-------------1---------ABC Software----123 main st1-------------2---------DEF Software-----2000 Long st1-------------3---------BOBs Software----3000 short ave2-------------4---------Sams of Utah-----654 maple ave2-------------5---------Dallas Ram-------999 leaf ctUsersusr_org_key----usr_loc_key---usr_key----usr_name1--------------1-------------1--------Bill Gates1--------------1--------------2-------Mary Smith1--------------1--------------3-------Mike Smith1--------------2--------------4-------Ken Wagon1--------------2--------------5-------Dave Spade1--------------2--------------6-------John Denver2--------------4--------------8-------Amy Grant2--------------5--------------9-------Lee GrantThe report :MicrosoftABC Software--123 main st bill gates--mary smith--mike smith (each name in seperate records) DEF Software--2000 long st--ken wagon---dave spade--john denverBobs software---3000 short ave----(no names, no one belongs to location 3)________________________________________________________________________Digital Researchsams of utah---654 maple ave---amy grantdallas ram--999 leaf ct----lee grantOf course each name is in seperate records, I cant space out the "report" here.Again, the grouping is Organizations, then locations, then users.ThanksMike |
 |
|
|
|
|
|
|
|