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
Solved

decode in oracle (ver 8.1) - check the value

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

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 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

765 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