Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist 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
View previous topic :: :: View next topic  
Author Message
sri_mf

Active User


Joined: 31 Aug 2006
Posts: 215
Location: India

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

Suresh Ponnusamy

Active User


Joined: 22 Feb 2008
Posts: 107
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: 156

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: 215
Location: India

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
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts sort records based on length exceeds ... maxsubrat DFSORT/ICETOOL 7 Wed Oct 04, 2017 4:48 pm
No new posts HEX value search in a DB2 query maxsubrat DB2 2 Wed Oct 04, 2017 3:04 pm
No new posts Addition of two records on 2 fields amar143 SYNCSORT 8 Tue Oct 03, 2017 11:14 am
No new posts Create procedure issues -628 when add... chandraBE DB2 1 Mon Sep 18, 2017 12:16 pm
No new posts Setting Font Type for XMIT satish.ms10 All Other Mainframe Topics 3 Fri Sep 15, 2017 10:01 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us