Portal | IBM Manuals | Downloads | Products | Refer | Info | Programs | JCLs | Forum Rules*| Site Map | Mainframe CD 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index
 
Register
 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index FAQ Search Memberlist Usergroups Profile Log in to check your private messages Log in
 
DB2 Query to get certain type of records first

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
Author Message
sri_mf

Active User


Joined: 31 Aug 2006
Posts: 206
Location: At my Desk

PostPosted: Tue Aug 05, 2008 10:46 am    Post subject: DB2 Query to get certain type of records first
Reply with quote

I have a table containing two columns C1 and C2.

C1
C
A
B
C
D
A
B
D
C

and C2 Contains some numeric data.

Now the required output should be like this

C1
C
C
C
A
A
B
B
D
D

Ie All the 'C' Type records first and then remaining records on sorted order.

Can anyone provide query for this scenario.
Back to top
View user's profile Send private message
References
Suresh Ponnusamy

Active User


Joined: 22 Feb 2008
Posts: 104
Location: New York

PostPosted: Tue Aug 05, 2008 6:54 pm    Post subject:
Reply with quote

Hi Sri,

Please try the below given.

TABLE - TABLE1
COLUMN - C1 AND C2

SELECT 1, A.C1, A.C2
FROM TABLE1 A
WHERE A.C1 = 'C'

UNION ALL

SELECT 2, B.C1, B.C2
FROM TABLE1 B
WHERE B.C1 <> 'C'
ORDER BY 1 ASC, 2 ASC

This query will give the result as follows

1 C1
-------------------------
1 C
1 C
1 C
2 A
2 A
2 B
2 B
2 D
2 D

If required, You can select column C2 alongwith C1.
Back to top
View user's profile Send private message
die7nadal

Active User


Joined: 23 Mar 2005
Posts: 154

PostPosted: Tue Aug 05, 2008 11:37 pm    Post subject:
Reply with quote

If you can make sure C1 doesn't contain X'00', then you can use this query that uses just one pass of the table.
Code:
SELECT C1,
       C2,
       CASE WHEN C1='C' THEN X'00' ELSE C1
       END AS SORT_COL
 FROM TABLE1
ORDER BY 3
WITH UR;
Back to top
View user's profile Send private message
sri_mf

Active User


Joined: 31 Aug 2006
Posts: 206
Location: At my Desk

PostPosted: Wed Aug 06, 2008 9:35 am    Post subject: Reply to: DB2 Query to get certain type of records first
Reply with quote

Thank you Suresh and Nadal for your replies.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1