Friday, September 03, 2004

DBAzine - An Irregular Sorting Requirement by Craig Mullins

DBAzine - An Irregular Sorting Requirement by Craig Mullins:
"An Irregular Sorting Requirement
by Craig S. Mullins

Sometimes the requirements of a particular application dictate that data needs to be sorted using some irregular collating sequence. These odd needs sometimes cause developers to sit and scratch their heads for hours, searching for ways to make DB2 do something that seems “unnatural.” But often you can create an answer just by understanding the problem and applying some creative SQL.

At this point, some of you might be asking “What the heck is he talking about?” Fair enough. Let’s look at an example to clarify the issue.

Assume that you have a table containing transactions, or some other type of interesting facts. The table has a CHAR(3) column containing the name of the day on which the transaction happened; let’s call this column DAY_NAME.

Now, let’s further assume that we want to write queries against this table that orders the results by DAY_NAME. We’d want Sunday first, followed by Monday, Tuesday, Wednesday, and so on. How can this be done?

"

No comments: