View previous topic :: View next topic
|
Author |
Message |
kalyan.v
New User
Joined: 04 Feb 2008 Posts: 65 Location: Hyd
|
|
|
|
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 |
|
|
vasanthz
Global Moderator
Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
|
|
|
|
Hello,
Could you show us your input data? |
|
Back to top |
|
|
kalyan.v
New User
Joined: 04 Feb 2008 Posts: 65 Location: Hyd
|
|
|
|
Input table data :
Code: |
COL1 COL2
---- ----
100 AAA
200 BBB
300 CCC
300 DDD
300 EEE
|
[/code] |
|
Back to top |
|
|
vasanthz
Global Moderator
Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
|
|
|
|
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 |
|
|
murugan_mf
Active User
Joined: 31 Jan 2008 Posts: 148 Location: Chennai, India
|
|
|
|
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 |
|
|
vadim vashchenko
New User
Joined: 21 Mar 2011 Posts: 13 Location: usa
|
|
|
|
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 |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
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 |
|
|
vasanthz
Global Moderator
Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
|
|
|
|
@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.
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 |
|
|
|