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: 1458
Location: Azeroth

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: 1458
Location: Azeroth

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: 1278
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: 1458
Location: Azeroth

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 Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts Execessive parameter issue Sumeendar JCL & VSAM 5 Mon Dec 19, 2016 4:35 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am


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