IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Column Ordering


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
kishpra

New User


Joined: 24 May 2010
Posts: 92
Location: Pune

PostPosted: Wed Feb 24, 2016 4:53 pm
Reply with quote

I have a table with below data

Emp No Text Data Dept Num
1234 Wired duty 001
1234 8283838-10 002
1236 L/E 23-Jan 003
1238 Pipes fitted 001
1236 L/E 01-Feb 004
1234 L/E 01-Feb 004
1234 Testing 002

I want the query output as using ORDER BY

Emp No Text Data Dept Num
1234 L/E 01-Feb 004
1234 8283838-10 002
1234 Testing 002
1234 Wired duty 001
1236 L/E 23-Jan 003
1236 L/E 01-Feb 004
1238 Pipes fitted 001

That is Group of records with same Emp No grouped together. And with the same Emp No, Text Data records beginning with 'L/E' for the same EMp No to be grouped together and for Text Data other than beginning with 'L/E' should be grouped by Dept Num e.g.
1234 8283838-10 002
1234 Testing 002
Back to top
View user's profile Send private message
RahulG31

Active User


Joined: 20 Dec 2014
Posts: 446
Location: USA

PostPosted: Wed Feb 24, 2016 9:53 pm
Reply with quote

What I can think of, is that you can include a case statement in your query like this:
Quote:
Select Emp_No, Text_Data,
CASE WHEN SUBSTR(Text_Data,1,3) = 'L/E'
THEN
' ' || Text_Data
ELSE
Text_Data
END AS SOMETHING
,Dept_Num
From MyTable
ORDER BY Emp_No, SOMETHING, Dept_Num

Since, you are prefixing Text_Data with a space only when 'L/E', the row containing 'L/E' will always be the first for each Emp_No record set.

*Note that you get 4 columns out of this query.

.
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Thu Feb 25, 2016 4:17 pm
Reply with quote

As you haven't used code tags to present your data nothing lines up. Why should reponders have to 'decode' your post in order to give an answer? Some may not even bother and it could be them that have the definitive answer for you. If you cannot be bothered, why should they?
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Column names in SYSIBM tables DB2 5
No new posts Can we Insert duplicates in Primary U... DB2 2
No new posts COnvert a column with mix of hex ,cha... DB2 5
No new posts Need SQL for two latest records havin... DB2 10
No new posts Issue while fetching a column(Nullabl... DB2 18
Search our Forums:

Back to Top