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 |
|
bcox
Starting Member
6 Posts |
Posted - 2004-09-29 : 12:31:52
|
| NEWBIE QUESTION:I am working on a typical SuperType / SubType model to use in building a flexible schema for an enterprise type application. I am not great at SQL and I need a query that flattens the data in the SubTypes based on the SuperType key.An example of the tables and table structures for a typical case where a [Site] entity is comprised of one, or more [GeographicBoundary] entities and each [GeographicBoundary] entity type will have many different purposes in the model is shown below.Using the example data shown below, what is the best query approach to return a single record with data from all SubTypes based on a single SiteID that looks like this:SiteCode, SiteDescription, CountryDescription, StateDescription, CityDescription 'HQ' 'Home Office' 'United States' 'North Carolina' 'Charlotte' Thanks in advance, I appreciate the help... seaqualnewbie... -----------------------------------------------------------------------------------------SITES:create table "lfoSites" ( "SiteID" int not null, "SiteCode" nvarchar(10) not null, "SiteDescription" nvarchar(40) not null, "MetropolitanServiceAreaID" int null, "BusinessRegionID" int null) ON 'PRIMARY' goalter table "lfoSites" add constraint "lfoSites_PK" primary key clustered ("SiteID") EXAMPLE DATA:SiteID = 1SiteCode = 'HQ'SiteDescription = 'Home Office'MetropolitanServiceAreaID = 34BusinessRegionID = 1-----------------------------------------------------------------------------------------ASSOCIATION: SiteGeographicBoundariescreate table "lfoSiteGeographicBoundaries" ( "SiteID" int not null, "GeographicBoundaryID" int not null) ON 'PRIMARY' goalter table "lfoSiteGeographicBoundaries" add constraint "lfoSiteGeographicBoundaries_PK" primary key clustered ("SiteID", "GeographicBoundaryID") goEXAMPLE DATA:SiteID GeographicBoundaryID 1 1 1 2 1 3-----------------------------------------------------------------------------------------SUPERTYPE: GEOGRAPHICBOUNDARIEScreate table "lfoGeographicBoundaries" ( "GeographicBoundaryID" int not null, "GeographicBoundaryTypeID" int not null) ON 'PRIMARY' goalter table "lfoGeographicBoundaries" add constraint "lfoGeographicBoundaries_PK" primary key clustered ("GeographicBoundaryID") goEXAMPLE DATA:GeographicBoundaryID GeographicBoundaryTypeID 1 1 (= Country Type) 2 2 (= State Type) 3 3 (= City Type)-----------------------------------------------------------------------------------------SUBTYPE: COUNTRIES: create table "lfoCountries" ( "GeographicBoundaryID" int not null, "CountryCode" nvarchar(10) not null, "CountryDescription" nvarchar(40) not null) ON 'PRIMARY' goalter table "lfoCountries" add constraint "lfoCountries_PK" primary key clustered ("GeographicBoundaryID") goEXAMPLE DATA:GeographicBoundaryID = 1CountryCode = 'USA'CountryDescription = 'United States'-----------------------------------------------------------------------------------------SUBTYPE: STATES:create table "lfoStates" ( "GeographicBoundaryID" int not null, "StateCode" nvarchar(10) not null, "StateDescription" nvarchar(40) not null) ON 'PRIMARY' goalter table "lfoStates" add constraint "lfoStates_PK" primary key clustered ("GeographicBoundaryID") goEXAMPLE DATA:GeographicBoundaryID = 2StateCode = 'NC'StateDescription = 'North Carolina'-----------------------------------------------------------------------------------------SUBTYPE: CITIES:create table "lfoCities" ( "GeographicBoundaryID" int not null, "CityCode" nvarchar(10) not null, "CityDescription" nvarchar(40) not null) ON 'PRIMARY' goalter table "lfoCities" add constraint "lfoCities_PK" primary key clustered ("GeographicBoundaryID") goEXAMPLE DATA:GeographicBoundaryID = 3CityCode = '22'CityDescription = 'Charlotte'----------------------------------------------------------------------------------------- |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-29 : 12:54:48
|
of the top of my head use left join:select ...lfoSiteGeographicBoundaries t1left join GEOGRAPHICBOUNDARIES t2 on t1.GeographicBoundaryID = t2.GeographicBoundaryIDleft join .... -- other tableswhere your condition.Go with the flow & have fun! Else fight the flow |
 |
|
|
bcox
Starting Member
6 Posts |
Posted - 2004-09-29 : 13:35:32
|
| I tried the following, but I get a ragged result. I need to flatten the result into a single record...SELECTsi.SiteID, si.SiteCode,si.SiteDescription, sg.GeographicBoundaryID,co.CountryDescription,st.StateDescription,ct.CityDescriptionFROM lfoSiteGeographicBoundaries sgLEFT JOIN lfoSites si ON sg.SiteID = si.SiteIDLEFT JOIN lfoCountries co ON sg.GeographicBoundaryID = co.GeographicBoundaryIDLEFT JOIN lfoStates st ON sg.GeographicBoundaryID = st.GeographicBoundaryIDLEFT JOIN lfoCities ct ON sg.GeographicBoundaryID = ct.GeographicBoundaryIDWHERE sg.SiteID = 1; |
 |
|
|
bcox
Starting Member
6 Posts |
Posted - 2004-09-29 : 13:41:02
|
| Example Results From the Last Post...1 HQ Home Office 1 NULL NULL NULL1 HQ Home Office 2 United States of America NULL NULL1 HQ Home Office 3 NULL NULL NULL1 HQ Home Office 4 NULL North Carolina NULL1 HQ Home Office 6 NULL NULL NULL1 HQ Home Office 7 NULL NULL Charlotte1 HQ Home Office 8 NULL NULL NULL1 HQ Home Office 9 NULL NULL NULL |
 |
|
|
|
|
|
|
|