Sometimes an application can require different, perhaps even somewhat “odd” data sorting. These needs may cause developers to sit and scratch their head for hours trying to make DB2 do something that seems “unnatural.” But often you can conjure up an answer by properly understanding the problem and applying some creative SQL.
Some of you might be asking “What the heck is he talking about?” That’s a fair question, so let’s look at an example to clarify.
Assume that you have a table containing transactions, or some other type of interesting information. 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?
Well, if we write the first query that comes to mind the results will obviously be sorted improperly:
SELECT DAY_NAME, COL1, COL2 . . .
ORDER BY DAY_NAME;
The results from this query would be ordered alphabetically; in other words:
This is what I mean be an irregular sorting requirement. The example may not be an everyday need, but it is not unrealistic for a business to have this, or a similar requirement that needs a different sort order than strictly alphabetical or numeric. So what is the solution here?
Of course, one solution would be to design the table with an additional numeric or alphabetic column that would sort properly. By this I mean that we could add a DAY_NUM column that would be 1 for Sunday, 2 for Monday, and so on. But this requires a database design change, and it becomes possible for the DAY_NUM and DAY_NAME to get out of sync.
There is another solution that is both elegant and does not require any change to the database. To implement this solution all you need is an understanding of SQL and SQL functions – in this case, the LOCATION function. Here is the SQL:
SELECT DAY_NAME, COL1, COL2 . . .
ORDER BY LOCATE(DAY_NAME, ‘SUNMONTUEWEDTHUFRISAT’);
The trick here is to understand how the LOCATE function works: It returns the starting position of the first occurrence of one string within another string.
So, in our example SQL, LOCATE finds the position of the DAY_NAME value within the string ‘SUNMONTUEWEDTHUFRISAT’, and returns the integer value of that position. So, if DAY_NAME is WED, the LOCATE function in the above SQL statement returns 10. Sunday would return 1, Monday 4, Tuesday 7, Wednesday 10, Thursday 13, Friday 16, and Saturday 19. This means that our results would be in the order we require.
(Note: Other database systems have a similar function called INSTR.)
Of course, you can go one step further if you’d like. Some queries may need to actually return the day of week. You can use the same technique with a twist to return the day of week value given only the day’s name. To turn this into the appropriate day of the week number (that is, a value of one through seven), we divide by three, use the INT function on the result to return only the integer portion of the result, and then add one:
INT(LOCATE(DAY_NAME, ‘SUNMONTUEWEDTHUFRISAT’)/3) + 1;
Let’s use our previous example of Wednesday again. The LOCATE function returns the value 10. So, INT(10/3) = 3 and add 1 to get 4. And sure enough, Wednesday is the fourth day of the week.
With a sound understanding of the features of DB2 SQL, many requirements that seem “odd” at first glance are achievable using nothing more than SQL and your imagination.
This post was originally published on Craig Mullin’s blog: http://db2portal.blogspot.com/2006/12/sorting-out-solution-in-db2.html
EPM applications help measure the business performance. This post will help you choose the best EPM solutions for your organization’s needs and objectives.
It’s 2015 and you can now establish totally respectable MS SQL DBA credibility just by mentioning you have been in the game since SQL Server version 9. You may even get the same gasps of shock from some colleagues that used to be reserved for the version 6 veterans.