Home » SQL & PL/SQL » SQL & PL/SQL » Simple Question
icon5.gif  Simple Question [message #609481] Fri, 07 March 2014 04:59 Go to next message
Crimzon
Messages: 3
Registered: March 2014
Location: Dubai
Junior Member

There are three columns A, B and Result :

Result column will show if A = B then Yes else No
A B Result
1 0 No
1 1 Yes
2 3 No
4 4 Yes

I want to write a select clause which will return me Result in third column.
But i want to write this in Select clause not in Where conditions. I tried with decode but it does not work kindly help me out.
Re: Simple Question [message #609482 is a reply to message #609481] Fri, 07 March 2014 05:06 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
decode works just fine, show us what you tried.

EDIT: complete word typo

[Updated on: Fri, 07 March 2014 05:41]

Report message to a moderator

Re: Simple Question [message #609483 is a reply to message #609482] Fri, 07 March 2014 05:25 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Use below code

WITH TAB AS (SELECT 1 AS A ,0 AS B FROM DUAL UNION ALL
SELECT 1 AS A ,1 AS B FROM DUAL UNION ALL
SELECT 2 AS A ,3 AS B FROM DUAL UNION ALL
SELECT 4 AS A ,4 AS B FROM DUAL)
SELECT a,b,CASE WHEN a=b then 'Yes' ELSE 'No' END AS result   FROM tab
/



Regards,
Anil MK
Re: Simple Question [message #609484 is a reply to message #609482] Fri, 07 March 2014 05:45 Go to previous messageGo to next message
Crimzon
Messages: 3
Registered: March 2014
Location: Dubai
Junior Member
After your positive glimpse on decode function,
i tried the following and it seems working now. Smile

Select a, b, decode(case
                    when a=b then '1'
                    when a<>b then '0'
                    end), '1', 'Yes', '0', 'No') Result

[Updated on: Fri, 07 March 2014 05:47]

Report message to a moderator

Re: Simple Question [message #609486 is a reply to message #609484] Fri, 07 March 2014 06:00 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Just:
select a, b, deoode(a, b, 'Yes', 'No') result
from mytab
/

Or the same with CASE.
You don't need to use both of them.

Previous Topic: unit calculation
Next Topic: create new record from LAST record with SQL
Goto Forum:
  


Current Time: Fri Apr 26 05:05:19 CDT 2024