MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL for ordering columns in 'irregular' collating sequence.

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
Cogito-Ergo-Sum
Advanced


Joined: 15 Dec 2002
Posts: 637
Topics: 43
Location: Bengaluru, INDIA

PostPosted: Wed Feb 01, 2006 12:11 pm    Post subject: SQL for ordering columns in 'irregular' collating sequence. Reply with quote

Hi,
Can someone please explain this SQL :

Code:
SELECT   DAY_NAME, COL1, COL2 . . .FROM
TXN_TABLE ORDER BY LOCATE(DAY_NAME,'SUNMONTUEWEDTHUFRISAT');


to sort the names of days in the chronological sequence rather than the
alphabetic sequence.

My confusion is with the LOCATE function. This will return values that will change with every value in the said column. So, how does the ORDER BY actually happen?

I found the SQL at http://www.dbazine.com/db2/db2-mfarticles/mullins-sort

I am not sure, I follow the explanation given in the article.
Quote:
"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 previously mentioned SQL statement returns 10. (Note: Some other database systems have a similar function called INSTR.) 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."

_________________
ALL opinions are welcome.

Debugging tip:
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
-- Sherlock Holmes.
Back to top
View user's profile Send private message
Bithead
Advanced


Joined: 03 Jan 2003
Posts: 550
Topics: 23
Location: Michigan, USA

PostPosted: Wed Feb 01, 2006 2:47 pm    Post subject: Reply with quote

There is a column called DAY_NAME with possible values of SUN, MON, TUE, WED, THU, FRI and SAT.

ORDER BY DAY_NAME would order it alphabetically i.e. FRI, MON, SAT, SUN, THU, TUE and WED.

To order it by day of week, use the LOCATE function as you described. The value for SUN would always be 1, MON would always be 4, TUE would be 7 and so on.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Wed Feb 01, 2006 3:08 pm    Post subject: Reply with quote

Cogito-Ergo-Sum,

I don't think that dynamic order by status is valid on Mainframe DB2

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Bithead
Advanced


Joined: 03 Jan 2003
Posts: 550
Topics: 23
Location: Michigan, USA

PostPosted: Wed Feb 01, 2006 4:42 pm    Post subject: Reply with quote

I ran a quick test and it worked. (DB2 v7)
Back to top
View user's profile Send private message
Cogito-Ergo-Sum
Advanced


Joined: 15 Dec 2002
Posts: 637
Topics: 43
Location: Bengaluru, INDIA

PostPosted: Wed Feb 01, 2006 11:12 pm    Post subject: Reply with quote

Bithead,
Bithead wrote:
There is a column called DAY_NAME with possible values of SUN, MON, TUE, WED, THU, FRI and SAT.

ORDER BY DAY_NAME would order it alphabetically i.e. FRI, MON, SAT, SUN, THU, TUE and WED.

To order it by day of week, use the LOCATE function as you described. The value for SUN would always be 1, MON would always be 4, TUE would be 7 and so on.


I understood that.

But, my confusion is with the ORDER BY LOCATE(...) part. As Kolusu, pointed the ORDER BY is dynamic in nature...
_________________
ALL opinions are welcome.

Debugging tip:
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
-- Sherlock Holmes.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Feb 02, 2006 8:16 am    Post subject: Reply with quote

Quote:

I ran a quick test and it worked. (DB2 v7)


Bithead,

Did you run the query which Cogito has provided in the first post? I don't think Day_name function is supported in DB2 unless you have the schema DSN8.

Can you show me the Query which you ran?

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Cogito-Ergo-Sum
Advanced


Joined: 15 Dec 2002
Posts: 637
Topics: 43
Location: Bengaluru, INDIA

PostPosted: Thu Feb 02, 2006 8:57 am    Post subject: Reply with quote

Kolusu,
Quote:
Did you run the query which Cogito has provided in the first post? I don't think Day_name function is supported in DB2 unless you have the schema DSN8.


The DAY_NAME is a column above not a function.
_________________
ALL opinions are welcome.

Debugging tip:
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
-- Sherlock Holmes.
Back to top
View user's profile Send private message
Bill Dennis
Advanced


Joined: 03 Dec 2002
Posts: 579
Topics: 1
Location: Iowa, USA

PostPosted: Thu Feb 02, 2006 12:05 pm    Post subject: Reply with quote

Cogito,
Are you still confused?

Your ORDER BY statement specifies the column DAY_NAME. Before sorting, the column has a LOCATE function performed on it where the values are converted to the offset into the list 'SUNMON.....SAT'.
_________________
Regards,
Bill Dennis

Disclaimer: My comments on this foorum are my own and do not represent the opinions or suggestions of any other person or business entity.
Back to top
View user's profile Send private message
Cogito-Ergo-Sum
Advanced


Joined: 15 Dec 2002
Posts: 637
Topics: 43
Location: Bengaluru, INDIA

PostPosted: Thu Feb 02, 2006 12:14 pm    Post subject: Reply with quote

Bill,
Are you suggesting that the above query is similar to
Code:
 SELECT DAY_NAME, LOCATE(DAY_NAME,'SUNMONTUEWEDTHUFRISAT') AS C1 FROM ABCD ORDER BY C1

?
_________________
ALL opinions are welcome.

Debugging tip:
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
-- Sherlock Holmes.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group