Solved

decode in oracle (ver 8.1) - check the value

Posted on 2007-03-30
8
1,083 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
8 Comments
 
LVL 4

Expert Comment

by:mukhtar2t
Comment Utility
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
Comment Utility
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
Comment Utility
is case supported in 8i?
0
 

Author Comment

by:mahjag
Comment Utility
I tried the solution you gave and I got error ORA-00937 - not a single group function
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 16

Expert Comment

by:MohanKNair
Comment Utility
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
Comment Utility
is the unit field on separate table and contains one row?
0
 
LVL 27

Accepted Solution

by:
sujith80 earned 50 total points
Comment Utility
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
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
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, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now