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

SELECT query issue


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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: 1742
Location: Tirupur, India

PostPosted: Thu Jun 09, 2011 6:38 pm
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
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: 1742
Location: Tirupur, India

PostPosted: Thu Jun 09, 2011 7:07 pm
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
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
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
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: 1742
Location: Tirupur, India

PostPosted: Mon Jun 13, 2011 11:37 am
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 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
No new posts Query on edit primary command CLIST & REXX 1
Search our Forums:

Back to Top