Hi,I am trying to write queries against a vendor's database whose tables are in a very peculiar format. They are neither fully normalized nor de-normalized. Here is an example of the structure:[code="sql"]DECLARE @Person TABLE ( ID int NOT NULL, FirstName varchar(25) NULL, LastName varchar(25) NULL, DOB date NULL)INSERT @Person (ID, FirstName, LastName, DOB) VALUES (1, N'John', N'Doe', CAST(N'1980-12-31' AS Date))INSERT @Person (ID, FirstName, LastName, DOB) VALUES (2, N'Sally', N'Smith', CAST(N'1981-09-15' AS Date))DECLARE @Person_Favorites TABLE ( ID int NOT NULL, POS int NOT NULL, Hobby varchar(50) NULL, Color varchar(50) NULL, SQLConference varchar(50) NULL)INSERT @Person_Favorites (ID, POS, Hobby, Color, SQLConference) VALUES (1, 1, N'Basketball', N'Blue', N'PASS Summit')INSERT @Person_Favorites (ID, POS, Hobby, Color, SQLConference) VALUES (1, 2, N'Baseball', N'Green', N'SQL Saturday')INSERT @Person_Favorites (ID, POS, Hobby, Color, SQLConference) VALUES (1, 3, NULL, N'Yellow', N'SQL Cruise')INSERT @Person_Favorites (ID, POS, Hobby, Color, SQLConference) VALUES (1, 4, NULL, N'Black', NULL)INSERT @Person_Favorites (ID, POS, Hobby, Color, SQLConference) VALUES (1, 5, NULL, N'Brown', NULL)INSERT @Person_Favorites (ID, POS, Hobby, Color, SQLConference) VALUES (2, 1, N'Tennis', N'Red', N'SQL Bits')INSERT @Person_Favorites (ID, POS, Hobby, Color, SQLConference) VALUES (2, 2, N'Football', N'Green', N'SQLIntersection')INSERT @Person_Favorites (ID, POS, Hobby, Color, SQLConference) VALUES (2, 3, N'Soccer', NULL, NULL)INSERT @Person_Favorites (ID, POS, Hobby, Color, SQLConference) VALUES (2, 4, N'Track and Field', NULL, NULL)SELECT *FROM @PersonSELECT *FROM @Person_Favorites[/code]You can see that the @Person_Favorites table has a column for each type of favorite for the person, and the number of rows each person will have is equal to highest count of all of their favorite things.So, if I wanted to report and filter on any of these, one of the things I thought to do is create views to normalize them. The queries would look something like:[code="sql"]SELECT ID, POS, HobbyFROM @Person_FavoritesWHERE Hobby IS NOT NULLSELECT ID, POS, ColorFROM @Person_FavoritesWHERE Color IS NOT NULLSELECT ID, POS, SQLConferenceFROM @Person_FavoritesWHERE SQLConference IS NOT NULL[/code]However, that would mean I'd have to create a ton of views if the person has 100 different favorite categories. In addition, I believe the query optimizer wouldn't be able to best optimize the query if I start joining several views together.I can't reasonably just join @Person to @Person_Favorites on ID because of how the data is stored in @Person_Favorites, right?I'm curious to know what others think. Does anyone know of a way to address this from a reporting perspective? Obviously a data warehouse is in the future but there are still real-time reports that need to be run.Thanks in advance,Mike
↧