Solved

decode in oracle (ver 8.1) - check the value

Posted on 2007-03-30
8
1,092 Views
Last Modified: 2010-08-05
I want to write a sql to check the value of a field greater than zero and then do min (start date) if the value (of unit ) greater than zero and also do max(end date) for the same condition if the units is greater than zero
I am inserting the start and end to another table so for start date I should check
if unit >0 then
min (start date)

similarly for end date I should check
if unit >0 then
max(end date)
0
Comment
Question by:mahjag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18825547
select
case  when unit>0 then min(start_date)  end start_date,
case  when unit>0 then max(end_date) end start_date
from your_table
 
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18825549
select
case  when unit>0 then min(start_date)  end start_date,
case  when unit>0 then max(end_date) end end_date
from your_table
 
0
 

Author Comment

by:mahjag
ID: 18825571
is case supported in 8i?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:mahjag
ID: 18825586
I tried the solution you gave and I got error ORA-00937 - not a single group function
0
 
LVL 16

Expert Comment

by:MohanKNair
ID: 18826349
select
case  when b.unit>0 then a.start_date  end start_date,
case  when b.unit>0 then a.end_date end end_date
FROM
(select id, min(start_date) start_date, max(end_date) end_date from mytable group by id) a,
mytable b
WHERE
a.id=b.id;
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18826687
is the unit field on separate table and contains one row?
0
 
LVL 27

Accepted Solution

by:
sujith80 earned 50 total points
ID: 18834399
You havent mentioned what columns are expected in the output. And what should be the output if unit = 0?

Use this query:

select unit, decode(sign(unit), 1, min(start_date) over(), NULL),
             decode(sign(unit), 1, max(end_date) over(), NULL)
from <your table>
/

An alternative is :

select X.unit, decode(sign(unit),1,st_dt, NULL), decode(sign(unit),1,end_dt, NULL)
from <your table> X,
(select min(start_date) st_dt, max(end_date) end_dt from <your table> ) Y
/
0
 

Author Comment

by:mahjag
ID: 18865052
Hi Sujith80 - need your help

unfortunately I need to reopen this as I wanted to incorporate this to already existing view and the select statement is giving problems, my view statement looks like this

select sum(wamount), sum(ramount),
          max(st_dt), min(end_dt),....
from (select col1 from table b where customer id = <idno>) h1,tableA, tableB..
where h1.col1 = tableA.col1
and ...

I want to remove the max(st_dt) and min (end_dt) with your solution .. any ideas how to incorporate this when there is already a in line sql using from clause as table name
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question