• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple output for a case condition

 
Mansi Mishra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am using PL SQL to write a query

Is it possible to have something like this

Case when (expression)
then (column 1) as column_one , (column 2)as column_two
else (column 3) as column_three
end

 
Jan Cumps
Bartender
Posts: 2599
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Mansi Mishra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:



Thanks Jan
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansi Mishra. wrote:
Is it possible to have something like this

Case when (expression)
then (column 1) as column_one , (column 2)as column_two
else (column 3) as column_three
end


Have you tried the above query ? I think we can use it to select conditional column names

 
Mansi Mishra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the above case query didn't work,
I couldn't find the syntax on the net either...
basically I wanted to fetch two or more columns when one of the case is matched...

 
Jan Cumps
Bartender
Posts: 2599
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you using sql or pl/sql?
In your initial mail you said pl/sql, but now it seems that you want to use this in an sql query.
You can't write (please correct me if I'm wrong) sql queries with a variable number of columns.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:
In your initial mail you said pl/sql, but now it seems that you want to use this in an sql query.

Sorry guys, I don't have any knowledge of PL/SQL
Jan Cumps wrote:
You can't write (please correct me if I'm wrong) sql queries with a variable number of columns.

You mean, we can't have this CASE statement in SELECT clause, then let me tell you, I had executed this query on SQL Server 05
 
Jan Cumps
Bartender
Posts: 2599
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean, we can't have this CASE statement in SELECT clause, then let me tell you, I had executed this query on SQL Server 05
No, that is not what I meant. I was refering to the topmost post.

I do have a question on it though. I know you can use case statement in a sql server query. But I think you can't use case to return two columns namedCOLUMN_ONE and COLUMN_TWO if the expression matches, and only one column named COLUMN_THREE if it does not match. What happens if expression is true for row 1, and false for row 2 in the query?

I was asking the original poster what the root of the problem is.
From the first post, I understood that that person was trying to use conditional logic in pl/sql.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:
You mean, we can't have this CASE statement in SELECT clause, then let me tell you, I had executed this query on SQL Server 05
No, that is not what I meant. I was refering to the topmost post.
What happens if expression is true for row 1, and false for row 2 in the query?

Oopps , my mistake again, not checking for multiple column, and told you that I can do it

Yes you are right, we can't use CASE condition for multiple columns in THEN condition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic