|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Question with 3 table query
I need to return 1 row of data where 3 tables are linked.
Table 1 (comp) is linked to Table 2 (cat) on Company ID Table 2 is linked to Table 3 (ref_cat) on CatID Table 1 contains the Company Information ie CompanyID Table 2 can have 1-3 listings for one CompanyID each with a unique CatID's (Industry, Category, Sub Category levels) Table 3 contains the unique CatID, a Parent ID, Name. The records that have a Parent ID of 0 are the Industry. The Category record is linked when the Parent ID is the same as the CatID of the top level. The Sub Category is linked when the Parent ID is the same as the CatID of the Category level. The Sql I have so far that does not return all the data on one row is below the table data. Here is what the table data looks like. Table 1 company data CompanyId - CompanyName 12345 - ABCDE 67890 - VWXYZ Table 2 category data CompanyId CategoryId 12345 - 1 12345 - 1002 12345 - 2330 67890 - 7 67890 - 1063 Table 3 ref_category data CategoryId - ParentId - Name 1 - 0 - Automotive 1002 - 1 - Auto Repair and Service 2001 - 1002 - All Repairs and Service 7 - 0 - Home Services 1063 - 7 - Landscape, Decks, Fencing My Code: SELECT comp.Name, cat.companyID, cat.CategoryID, ref_cat.categoryID, ref_cat.parentID, ref_cat.name FROM company as comp join category as cat on cat.companyid = comp.companyid join ref_category as ref_cat on ref_cat.categoryId = cat.categoryID Where comp.companyID in (2013, 2947) Order by comp.CompanyId |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Question with 3 table query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|