Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Venkata Ravi .

Asked: July 27, 2018 - 5:05 pm UTC

Last updated: July 31, 2018 - 9:46 am UTC

Version: ORACLE 12C

Viewed 1000+ times

You Asked

Hi sir,
I have one dought is given below
How to write select statement for bitmap index
For example
North is decode as 1 remains 0
West is decode as 1 remains 0
East is decode as 1 remains 0
South is decode as 1 remains 0
Output :-
N W E S
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

So please syntax for this problem. ...

with LiveSQL Test Case:

and Connor said...

You have not given us any context, any background , and explanation for your request :-(

I can do this - but we have no idea if that is what you want or something else.

select 1 north, 0 west, 0 east, 0 south from dual union all
select 0 north, 1 west, 0 east, 0 south from dual union all
select 0 north, 0 west, 1 east, 0 south from dual union all
select 0 north, 0 west, 0 east, 1 south from dual;

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Bitmap representation

Rajeshwaran, Jeyabal, July 30, 2018 - 11:03 am UTC

....
How to write select statement for bitmap index
....


Hope the OP was looking for something like this.

Lets say we have an EMP table and creating a BITMAP index on the JOB column, internally looks like this (a series of bitmap, where 0 represents absence of value and 1 represents the presence of value).

demo@ORA12C> col bits format a40
demo@ORA12C> select job, trim('-' from sys_connect_by_path( x ,'-') ) bits
  2  from (
  3  select e2.job,
  4      decode(e2.job,e1.job,1,0) x, r
  5  from ( select row_number() over(order by empno) r, job from emp ) e1,
  6       ( select distinct job from emp ) e2
  7       )
  8  where connect_by_isleaf = 1
  9  start with r = 1
 10  connect by prior job = job
 11    and prior r = r-1
 12  /

JOB       BITS
--------- ----------------------------------------
ANALYST   0-0-0-0-0-0-0-1-0-0-0-0-1-0
CLERK     1-0-0-0-0-0-0-0-0-0-1-1-0-1
MANAGER   0-0-0-1-0-1-1-0-0-0-0-0-0-0
PRESIDENT 0-0-0-0-0-0-0-0-1-0-0-0-0-0
SALESMAN  0-1-1-0-1-0-0-0-0-1-0-0-0-0

demo@ORA12C>

Chris Saxon
July 31, 2018 - 9:45 am UTC

Perhaps, I suspect Stew's decode below is closer to what they're looking for though...

For what it's worth

Stew Ashton, July 30, 2018 - 7:30 pm UTC

Concerning the "bitmap" on the EMP table, I'm not sure a hierarchical query is the most efficient solution. An alternative:
select j.job,
  listagg(decode(j.job, e.job, 1, 0)) within group(order by e.rowid) bitmap
from emp e,
(select distinct job from emp) j
group by j.job;

JOB         BITMAP           
ANALYST     00000001000010   
CLERK       10000000001101   
MANAGER     00010110000000   
PRESIDENT   00000000100000   
SALESMAN    01101000010000 
As far as the original question is concerned, maybe the OP wants help with decode?
with data(compass_point) as (
  select 'N' from dual
  union all
  select 'E' from dual
  union all
  select 'S' from dual
  union all
  select 'W' from dual
)
select decode(compass_point,'N',1,0) N,
  decode(compass_point,'E',1,0) E,
  decode(compass_point,'S',1,0) S,
  decode(compass_point,'W',1,0) W
from data;

N   E   S   W   
1   0   0   0 
0   1   0   0 
0   0   1   0 
0   0   0   1 
Best regards,
Stew
Chris Saxon
July 31, 2018 - 9:46 am UTC

Yep, the cross join-listagg method will come out better than the recursive way.

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.