November 20, 2001 05:20 PM

Querying a Real-Life Denormalized Table

Rating: (0)
SQL Server Magazine
InstantDoc ID #22987

In real life, you often have to work with data in tables that aren't properly normalized. For example, let's say you want to list in alphabetical order all the residents of an apartment building. This task would be easy with a normalized database, which would have one table to hold just the names of individual residents, each of whom might or might not share an apartment unit with someone else. But in our real-life scenario, the database developer (unwisely) created a table for each apartment, using an array, or repeating group, to store each resident's name.

Listing 1 shows the denormalized Id_Residents table and all its columns. Ugh! The developer thought that because no more than five residents would live in any one apartment (location_id) at a time, creating a repeating group was OK. But suppose someone requests an alphabetized view of all residents in the entire building (as happened to me). How do you create this view without using temporary tables, cursors, or other resource-hungry tools?

My solution was to use the UNION statement along with a subquery that produces a derived table, as the code in Listing 2 shows. First, the subquery creates a derived table that consists of all the names in the Id_Residents table joined by the UNION statement. (Note the alias after the subquery; the alias is necessary when you're using derived tables.) The outer query then selects from this derived table and orders the data alphabetically. Note that the TOP n statement in the view definition is necessary in a view whenever you're using ORDER BY.

ARTICLE TOOLS

Add a Comment

There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS

eBooks

Playbook for a Virtualized Datacenter | During challenging times, optimizing the IT infrastructure becomes imperative. Many organizations are looking to extend their virtualization efforts to encompass the entire datacenter. Get a step-through of your technology options and more.

White Papers

As the advances in USB devices have made them invaluable to most business users’, they have also exposed organizations to enormous risks. Learn how to easily enforce device/port control and data encryption policies without requiring new infrastructure and additional admin overhead.

WEB SEMINARS

Is Flexible Lockdown Possible? Join Darren Mar-Elia for this free web webinar exploring the various methods you should consider to protect and control your desktops while also understanding the impact on the end user community.
View Seminar On-Demand.

eLearning Series

Windows IT Pro brings the experts direct to you to share their real-world perspective, experience, and expertise. During each event, three sessions stream in real time, allowing you to learn, to ask questions, and to get solutions.
Upcoming event: SQL Server Consolidation

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.