Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

November 20, 2001 12:00 AM

Querying a Real-Life Denormalized Table

SQL Server Pro
InstantDoc ID #22987
Rating: (0)
Downloads
22987.zip

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.

Related Content:

ARTICLE TOOLS

Comments
    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

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

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.