473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is there is a way in DB2 to return multiple fields from a single case

Hi everybody!

I am truing to concstruct something like this:

SELECT id, name, role,
CASE
WHEN role = 1 THEN SELECT item1, item2, item3 FROM table1
WHEN role = 2 THEN SELECT item1, item2, item3 FROM table2
ELSE SELECT item1, item2, item3 FROM table3
END as THIS, THAT, THOSE
FROM roles_tbl
Is this is valid in any DB2 VERSIONS

--
Message posted via http://www.dbmonster.com

Jun 27 '08 #1
4 3851
lenygold via DBMonster.com wrote:
Hi everybody!

I am truing to concstruct something like this:

SELECT id, name, role,
CASE
WHEN role = 1 THEN SELECT item1, item2, item3 FROM table1
WHEN role = 2 THEN SELECT item1, item2, item3 FROM table2
ELSE SELECT item1, item2, item3 FROM table3
END as THIS, THAT, THOSE
FROM roles_tbl
SELECT item1, item2, item3 FROM table1 WHERE role = 1
UNION ALL
SELECT item1, item2, item3 FROM table2 WHERE role = 2
....

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Jun 27 '08 #2
lenygold via DBMonster.com wrote:
I am truing to concstruct something like this:

SELECT id, name, role,
CASE
WHEN role = 1 THEN SELECT item1, item2, item3 FROM table1
WHEN role = 2 THEN SELECT item1, item2, item3 FROM table2
ELSE SELECT item1, item2, item3 FROM table3
END as THIS, THAT, THOSE
FROM roles_tbl
Is this is valid in any DB2 VERSIONS
Well, there are ways. This works:

with t1 as (SELECT item1, item2, item3 FROM table1),
t2 as (SELECT item1, item2, item3 FROM table2),
t3 as (SELECT item1, item2, item3 FROM table3),
SELECT id, name, role, coalesce(t1.item1, t2.item1, t3.item1) as
THIS,
coalesce(t1.item2, t2.item2, t3.item2) as THAT,
coalesce(t1.item3, t2.item3, t3.item3) as THOSE
FROM roles_tbl
LEFT OUTER JOIN t1 ON roles_tbl.role = 1
LEFT OUTER JOIN t2 ON roles_tbl.role = 2
LEFT OUTER JOIN t3 ON roles_tbl.role NOT IN (1, 2)

-Chris
Jun 27 '08 #3
>Is this is valid in any DB2 VERSIONS <<

This is not valid anywhere; CASE is an expression and not a control
flow statement. Use a UNION to get data from multiple tables.
Jun 27 '08 #4
I think that CTEs are not neccesary.
You can join the tables in FROM clause.

SELECT rt.id, rt.name, rt.role
, COALESCE(t1.item1, t2.item1, t3.item1) AS THIS
, COALESCE(t1.item2, t2.item2, t3.item2) AS THAT
, COALESCE(t1.item3, t2.item3, t3.item3) AS THOSE
FROM roles_tbl rt
LEFT JOIN
table1 t1
ON <some predicates to relate table1 with roles_tbl>
AND rt.role = 1
LEFT JOIN
table2 t2
ON <some predicates to relate table2 with roles_tbl>
AND rt.role = 2
LEFT JOIN
table3 t3
ON <some predicates to relate table3 with roles_tbl>
AND rt.role NOT IN (1, 2)
;
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
0
by: rayone | last post by:
Hi folks. I need advice. 2 options, which do you think is the better option to display/retrieve/report on the data. Keep in mind reporting (Crystal), SQL Performance, VB Code, usability,...
5
by: D. Shane Fowlkes | last post by:
This may be a very basic question but it's something I've never done before. I've looked at a couple of my favorite sites and books and can't find an answer either. I can write a Function to...
4
by: HLCruz via AccessMonster.com | last post by:
I am working with a database that has client information separated in to 4 related tables - tFolder, tAddress, tEmail, tPhone number. In addition there are related tables tGifts and tCalls. The...
13
by: cppquester | last post by:
A colleague told me that there is a rule about good stype that a function in C++ should have only one point of return (ie. return statement). Otherwise there might be trouble. I never heard about...
1
by: tjm0713 | last post by:
Not sure this can be done but here is my current situation. I have a table containing millions of records. Each record has 28 fields. One of the fields I am trying to return the single record for...
3
by: DeanL | last post by:
Hi guys, Does anyone know of a way to create multiple tables using information stored in one table? I have a table with 4 columns (TableName, ColumnName, DataType, DataSize) and wanted to know...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.