Solved

decode in oracle (ver 8.1) - check the value

Posted on 2007-03-30
8
1,084 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
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
 

Author Comment

by:mahjag
ID: 18825586
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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pl/SQL Query 31 76
Array of Key/Value Pairs as input to Oracle function 10 53
statspack purge automate 7 53
Getting TNS:Connect timeout occurred while opening the application 5 59
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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.
This video shows how to recover a database from a user managed backup

920 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

12 Experts available now in Live!

Get 1:1 Help Now