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

Active User


Joined: 31 Aug 2006
Posts: 216
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: 216
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 JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts Join giving more records than expected Danielle.Filteau SYNCSORT 2 Thu Mar 09, 2017 3:28 am
No new posts TSO or command line utility to genera... kishpra JCL & VSAM 3 Thu Mar 09, 2017 1:11 am
No new posts Extract set of records matching on ac... bhaskar_kanteti DFSORT/ICETOOL 3 Mon Mar 06, 2017 7:19 am
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us