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
 

 

SELECT query issue

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
kalyan.v

New User


Joined: 04 Feb 2008
Posts: 65
Location: Hyd

PostPosted: Thu Jun 09, 2011 5:56 pm    Post subject: SELECT query issue
Reply with quote

Hi,

I have a select query like below.

Code:

SELECT COL1, COL2 FROM TABLE1
WHERE COL1 IN ( '100', '200', '300', '400', '500')



Let say i got the output like below if table contains only 5 matching records.

Output:

Code:


COL1        COL2
-------    -------
100           AAA
200           BBB
300           CCC
300           DDD
300           EEE



I want output like below where keys - '400', '500' also should include in the result with some comments like mentioned.

Required output:

Code:


COL1        COL2
-------    -------
100           AAA
200           BBB
300           CCC
300           DDD
300           EEE
400           -no data-
500           -no data-




Can we get the output like that??

Thx,
Kalyan V
Back to top
View user's profile Send private message

vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1504
Location: Chennai

PostPosted: Thu Jun 09, 2011 6:38 pm    Post subject:
Reply with quote

Hello,
Could you show us your input data?
Back to top
View user's profile Send private message
kalyan.v

New User


Joined: 04 Feb 2008
Posts: 65
Location: Hyd

PostPosted: Thu Jun 09, 2011 6:56 pm    Post subject:
Reply with quote

Input table data :
Code:

COL1          COL2
----          ----
100           AAA
200           BBB
300           CCC
300           DDD
300           EEE
[/code]
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1504
Location: Chennai

PostPosted: Thu Jun 09, 2011 7:07 pm    Post subject:
Reply with quote

Hello,

You could have one more reference table and perform join on the reference table.

Code:
Table 1:
col1 col2
---- ----
100  AAA
200  BBB
300  CCC
300  DDD
300  EEE

Table 2:
col3 col4
---- ----
100 -no-data-
200 -no-data-
300 -no-data-
400 -no-data-
500 -no-data-

SELECT col3 as col1,coalesce(col2,col4) as col2 FROM TABLE1
right join                                                 
table2 on col1 = col3;


Would this suffice the requirement?
Back to top
View user's profile Send private message
murugan_mf

Active User


Joined: 31 Jan 2008
Posts: 148
Location: Chennai, India

PostPosted: Thu Jun 09, 2011 7:19 pm    Post subject: Reply to: SELECT query issue
Reply with quote

Kalyan,
Are you using this query in the program? I mean 100,200..are host varaibles?
If it is an embedded sql in a program why can't you handle this in the program?
Can you explain the scenario why you need like this?
Back to top
View user's profile Send private message
vadim vashchenko

New User


Joined: 21 Mar 2011
Posts: 13
Location: usa

PostPosted: Thu Jun 09, 2011 10:59 pm    Post subject:
Reply with quote

I wonder if the following would work:

1. Use outer join in the inline view
2. Use IFNULL, '-NO DATA-' on COL2.

select col1, ifnull(col2,'-no data-')
from (select col1 from ... left outer join ...)
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri Jun 10, 2011 1:41 pm    Post subject:
Reply with quote

like vasanthz said
if you have a table containing these : '100', '200', '300', '400', '500'
you can left join with it and use that

otherwise you can create that reference table inside your sql

Code:
select A.col1,coalesce(B.col1,'-No data-') from
(select 100 as col1 from sysibm.sysdummy1 union all
 select 200 as col1 from sysibm.sysdummy1 union all
 select 300 as col1 from sysibm.sysdummy1 union all
 select 400 as col1 from sysibm.sysdummy1 union all
 select 500 as col1 from sysibm.sysdummy1
) A
left join Table1 B on A.col1 = B.col1
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1504
Location: Chennai

PostPosted: Mon Jun 13, 2011 11:37 am    Post subject:
Reply with quote

@TS People put their time and effort to respond to a topic, but the TS don't even care to respond even after a straight forward working & tested code is provided. icon_confused.gif

A response like "thank you it worked" "no I have so & so problem with the code" would be encouraging.

@GuyC Thanks for your slick technique.
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 SQL - select data available in index Nileshkul DB2 3 Mon Jun 26, 2017 1:30 am
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts Issue with NDM process to transmit ES... chetanambi All Other Mainframe Topics 6 Wed May 03, 2017 10:52 am
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 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