Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Select only records where column values are not all equal to zero

oramark14Mar 1 2012 — edited Mar 1 2012
Hi everyone, this seems so easy but it's got me stumped on finding a clean, easy way to accomplish this. I'm sure when someone responds, I'll kick myself. So, assume the following is what I've got:
with mytable as (
select 'Type 1' as itemtype, 'JAN' as monthname, 0 as theval from dual union all
select 'Type 1' as itemtype, 'FEB' as monthname, 1 as theval from dual union all
select 'Type 1' as itemtype, 'MAR' as monthname, 5 as theval from dual union all
select 'Type 1' as itemtype, 'APR' as monthname, 1 as theval from dual union all
select 'Type 1' as itemtype, 'MAY' as monthname, 4 as theval from dual union all
select 'Type 1' as itemtype, 'JUL' as monthname, 0 as theval from dual union all
select 'Type 1' as itemtype, 'AUG' as monthname, 0 as theval from dual union all
select 'Type 1' as itemtype, 'SEP' as monthname, 1 as theval from dual union all
select 'Type 1' as itemtype, 'OCT' as monthname, 7 as theval from dual union all
select 'Type 1' as itemtype, 'NOV' as monthname, 1 as theval from dual union all
select 'Type 1' as itemtype, 'DEC' as monthname, 2 as theval from dual union all

select 'Type 2' as itemtype, 'JAN' as monthname, 0 as theval from dual union all
select 'Type 2' as itemtype, 'FEB' as monthname, 0 as theval from dual union all
select 'Type 2' as itemtype, 'MAR' as monthname, 0 as theval from dual union all
select 'Type 2' as itemtype, 'APR' as monthname, 0 as theval from dual union all
select 'Type 2' as itemtype, 'MAY' as monthname, 0 as theval from dual union all
select 'Type 2' as itemtype, 'OCT' as monthname, 0 as theval from dual union all
select 'Type 2' as itemtype, 'NOV' as monthname, 0 as theval from dual union all
select 'Type 2' as itemtype, 'DEC' as monthname, 0 as theval from dual
)
select
  itemtype,
  sum (case monthname when 'JAN' then theval else 0 end) as JAN,
  sum (case monthname when 'FEB' then theval else 0 end) as FEB,
  sum (case monthname when 'MAR' then theval else 0 end) as MAR,
  sum (case monthname when 'APR' then theval else 0 end) as APR,
  sum (case monthname when 'MAY' then theval else 0 end) as MAY,
  sum (case monthname when 'JUN' then theval else 0 end) as JUN,
  sum (case monthname when 'JUL' then theval else 0 end) as JUL,
  sum (case monthname when 'AUG' then theval else 0 end) as AUG,
  sum (case monthname when 'SEP' then theval else 0 end) as SEP,
  sum (case monthname when 'OCT' then theval else 0 end) as OCT,
  sum (case monthname when 'NOV' then theval else 0 end) as NOV,
  sum (case monthname when 'DEC' then theval else 0 end) as DEC
from mytable
group by itemtype
order by itemtype
I need an outer query around this one or something that will only select "Type 1"... i.e., if all of the months are each equal to zero, don't include the record in the result set.

Summing them to get a total of zero is not an option, as I could have -15 and +15 in different columns, in which case, that record would need to be displayed.

Something as simple as... "where not (oct = 0 and nov = 0 and dec = 0...) at the end is all I seem to need. I've thought about adding a case clause for each column, but that doesn't seem very efficient. Ideas?

Thanks in advance!
Mark

Edit... I know the following will work using the MINUS operator, but my real query is really huge, and I don't want to have to write it twice...
{code}
select
itemtype,
sum (case monthname when 'JAN' then theval else 0 end) as JAN,
sum (case monthname when 'FEB' then theval else 0 end) as FEB,
sum (case monthname when 'MAR' then theval else 0 end) as MAR,
sum (case monthname when 'APR' then theval else 0 end) as APR,
sum (case monthname when 'MAY' then theval else 0 end) as MAY,
sum (case monthname when 'JUN' then theval else 0 end) as JUN,
sum (case monthname when 'JUL' then theval else 0 end) as JUL,
sum (case monthname when 'AUG' then theval else 0 end) as AUG,
sum (case monthname when 'SEP' then theval else 0 end) as SEP,
sum (case monthname when 'OCT' then theval else 0 end) as OCT,
sum (case monthname when 'NOV' then theval else 0 end) as NOV,
sum (case monthname when 'DEC' then theval else 0 end) as DEC
from mytable
group by itemtype

minus

select
itemtype,
jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec
from (
select
itemtype,
sum (case monthname when 'JAN' then theval else 0 end) as JAN,
sum (case monthname when 'FEB' then theval else 0 end) as FEB,
sum (case monthname when 'MAR' then theval else 0 end) as MAR,
sum (case monthname when 'APR' then theval else 0 end) as APR,
sum (case monthname when 'MAY' then theval else 0 end) as MAY,
sum (case monthname when 'JUN' then theval else 0 end) as JUN,
sum (case monthname when 'JUL' then theval else 0 end) as JUL,
sum (case monthname when 'AUG' then theval else 0 end) as AUG,
sum (case monthname when 'SEP' then theval else 0 end) as SEP,
sum (case monthname when 'OCT' then theval else 0 end) as OCT,
sum (case monthname when 'NOV' then theval else 0 end) as NOV,
sum (case monthname when 'DEC' then theval else 0 end) as DEC
from mytable
group by itemtype
)
where ( oct = 0 and nov = 0 and dec = 0 and jan = 0 and feb = 0 and mar = 0
and apr = 0 and may = 0 and jun = 0 and jul = 0 and aug = 0 and sep = 0
)
order by itemtype
{code}

Edit again... Ok, I guess I'm answering my own question here, but I think using a WITH clause to write the main query once and then selecting * from it twice using the MINUS operator in between where the second query has where (oct = 0, etc.) is what I need. If anyone else has better suggestions, please do let me know! Here's the pseudo logic for what I've come up with so far...

{code}
WITH mainquery as (select everything)
select * from mainquery
minus
select * from mainquery where (oct = 0, nov = 0, etc...)
{code}

Thanks again!
Mark

Edited by: user455268 on Mar 1, 2012 7:13 PM

Edited by: user455268 on Mar 1, 2012 7:16 PM
This post has been answered by Frank Kulash on Mar 1 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 29 2012
Added on Mar 1 2012
2 comments
1,424 views