Solved

decode and sign sql ..

Posted on 2007-04-06
4
1,338 Views
Last Modified: 2009-12-27
I had to re-open an already closed question where sujith80 gave a solution for decode problem
Problem description
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)

Solution proposed
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
/

I applied this solution and having a problem, in the above decode if the unit is >0 then the dates min and max worked, in the data there are units <0 rows that are existing and it shows up as null start date (st_dt) and null end date (end_dt), since I am writing a view statement from this query I do not wan them to appear in the view and also I want only for the date sake to fetch the min and max dates for unit >0, but if the rows has units <0 I dont want them to show up and the total amount should have included the rows having units <0

for example if I have 3 rows as data set and let us they are
unit            start date               end date
100          01-jan-07                 31-jan-07
200          01-feb-07                 28-feb-07
-100         01-mar-07                 31-mar-07
then for the start and end dates I need to get min (start date) and max ( end date) for unit >0
01-jan-07 and end date 28-feb-07
and the total should be 300-100 = 200
0
Comment
Question by:mahjag
  • 3
4 Comments
 

Author Comment

by:mahjag
ID: 18865498
So i want to add my expected result - view having columns

Total unit         start date           end date
200                   01-jan-07         28-feb-07
0
 

Author Comment

by:mahjag
ID: 18865625
Looks like I got away with the problem by doing this

select max(decode(sign(unit),1,st_dt, NULL), decode(sign(unit),1,end_dt, NULL))
to avoid group by the whole decode statement and now I got what I want - but it looks to me as pseudo solution by wrapping the aggregate max over the decode and sign as it means nothing.. I will wait for folks to jump in on the solution - nevertheless I found the above work around..
0
 
LVL 32

Accepted Solution

by:
awking00 earned 125 total points
ID: 18866329
select sum(u.unit), x.st_dt, x.end_dt
from your_tbl u,
(select min(st_dt) st_dt
       ,max(end_dt) end_dt
 from your_tbl
 where unit > 0) x
group by x.st_dt, x.end_dt;
0
 

Author Comment

by:mahjag
ID: 18867088
Hi awking00

I ran the sql and I got the row that has amount = 0, I am trying to avoid this ..
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 Multiple PAYER_IDs with PAYER_TYPES are these types 3 39
EXECUTE IMMEDIATE 5 53
case statement in where clause with not exist 15 46
Oracle SQL 6 48
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

863 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

23 Experts available now in Live!

Get 1:1 Help Now