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

DB2 Query to get certain type of records first


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

Active User


Joined: 31 Aug 2006
Posts: 218
Location: India

PostPosted: Tue Aug 05, 2008 10:46 am
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
Suresh Ponnusamy

Active User


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

PostPosted: Tue Aug 05, 2008 6:54 pm
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: 156

PostPosted: Tue Aug 05, 2008 11:37 pm
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: 218
Location: India

PostPosted: Wed Aug 06, 2008 9:35 am
Reply with quote

Thank you Suresh and Nadal for your replies.
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 Compare only first records of the fil... SYNCSORT 7
No new posts Pulling a fixed number of records fro... DB2 2
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
Search our Forums:

Back to Top