2010-03-26

Pivoting EAV

If you are responsible for designing a data model and just consider to invent again and create this fine generic entity attribute value structure, maybe you should consider attending some teaching about the issue. For example some available soon by C.J. Date in and near Finland.

Well maybe you have a EAV model that you have to deal with. Example


SQL> create table eav as
2 select 1 e, 'first' a, 'Timo' v from dual union all
3 select 1 e, 'last' a, 'Raitalaakso' v from dual union all
4 select 1 e, 'nic' a, 'Rafu' v from dual union all
5 select 2 e, 'first' a, 'John' v from dual union all
6 select 2 e, 'last' a, 'Doe' v from dual
7 ;

Table created.

SQL> select * from eav;

E A V
---------- ----- -----------
1 first Timo
1 last Raitalaakso
1 nic Rafu
2 first John
2 last Doe


You should not query it in a basic case using joins. Most possibly you have tens of joins to the same table.


SQL> select la.e, fi.v firs, la.v las
2 from eav la, eav fi
3 where la.e=fi.e
4 and fi.a='first'
5 and la.a='last'
6 ;

E FIRS LAS
---------- ----------- -----------
1 Timo Raitalaakso
2 John Doe


It is a pivot you want to do.

SQL> select e, firs, las
2 from eav
3 pivot (max(v) for a in ('first' as firs, 'last' as las))
4 ;

E FIRS LAS
---------- ----------- -----------
1 Timo Raitalaakso
2 John Doe


With the pivot you get the nullable columns also easier without filtering out the whole entity

SQL> select e, firs, las, ni
2 from eav
3 pivot (max(v) for a in ('first' as firs, 'last' as las, 'nic' as ni))
4 ;

E FIRS LAS NI
---------- ----------- ----------- -----------
1 Timo Raitalaakso Rafu
2 John Doe


Maybe you do not have 11g features available.

SQL> select e
2 , max(case when a = 'first' then v end) firs
3 , max(case when a = 'last' then v end) las
4 from eav
5 group by e
6 ;

E FIRS LAS
---------- ----------- -----------
1 Timo Raitalaakso
2 John Doe


And the best thing to do with it might be.

SQL> drop table eav purge;

Table dropped.

2010-03-11

Parallel query distribution methods

Figuring out should I recommend parallelizing materialized view refresh for several mvs or use parallel partition wise joins. Found valuable help for the issue just yesterday by Tony Hasler. Will be using both. Unfortunately no blog writing coming up about the issue.

2010-03-05

not in null countdown

Just a reminder about not in and nulls. Maybe consider using not exists or anti join if any of the columns in not in list may be null. Or maybe one of the following might be the result you want.


SQL> create table nm as
2 with le as (select level ev from dual connect by level<4)
3 select l.ev e,e.ev v from le l, le e;

Table created.

SQL> select count(*) from nm;

COUNT(*)
----------
9

SQL> select count(*) from nm where (e,v) not in ((1,1));

COUNT(*)
----------
8

SQL> select count(*) from nm where (e,v) not in ((1,1),(2,2));

COUNT(*)
----------
7

SQL> select count(*) from nm where (e,v) not in ((1,null));

COUNT(*)
----------
6

SQL> select count(*) from nm where (e,v) not in ((1,1),(2,null));

COUNT(*)
----------
5

SQL> select count(*) from nm where (e,v) not in ((null,1),(2,null));

COUNT(*)
----------
4

SQL> select count(*) from nm where (e,v) not in ((1,null),(2,null));

COUNT(*)
----------
3

SQL> select count(*) from nm where (e,v) not in ((1,null),(2,null),(3,3));

COUNT(*)
----------
2

SQL> select count(*) from nm where (e,v) not in ((null,1),(null,2),(1,3),(2,3));

COUNT(*)
----------
1

SQL> select count(*) from nm where (e,v) not in ((null,null),(1,1));

COUNT(*)
----------
0

SQL> select count(*)
2 from nm m
3 where not exists (select n
4 from (select null n from dual) d
5 where m.v = d.n
6 and m.e = d.n);

COUNT(*)
----------
9

SQL> select count(*)
2 from nm m
3 left outer join (select 10 e, null n from dual) d
4 on m.e=d.e
5 where d.n is null;

COUNT(*)
----------
9

2010-02-14

Data types in a view

Is there actually a TIME data type in Oracle? What is the data type of null? Boolean is a number or is it?


SQL> create view v as
2 select null nul
3 , time '12:00:00' tim
4 , dbms_session.is_role_enabled('CREATE SESSION') boo
5 from dual
6 ;

View created.

SQL>
SQL> select column_name,data_type,data_length
2 from user_tab_columns
3 where table_name = 'V'
4 order by 1
5 ;

COLUMN_NAME DATA_TYPE DATA_LENGTH
------------ ------------ -----------
BOO NUMBER 22
NUL VARCHAR2 0
TIM TIME(9) 20

SQL>
SQL> select nul from v;

N
-


SQL>
SQL> select tim from v;

TIM
--------------------------------------------------------------
12:00:00,000000000

SQL>
SQL> select boo from v;
select boo from v
*
ERROR at line 1:
ORA-06552: PL/SQL: Statement ignored
ORA-06553: PLS-382: expression is of wrong type



null and time types from Laurent Schneider.

2010-02-12

Open position

It has been a while since Pirjo Salo wrote about job opportunities for database specialist in Finland. Be aware something to be available soon.

2010-02-08

Equality -comparing text

Should we use case clause or still use decode? Yet again someting to be aware. Seems like case clause is trimming before comparing. And the same with DB2 minus. Postgres does not trim.

Oracle


SQL> select case when 'a' = 'a ' then 'same' else 'different' end as test from dual;

TEST
---------
same

SQL> select 'a' from dual minus select 'a ' from dual;

'A
--
a

SQL> select decode('a','a ','same','different') testdecode from dual;

TESTDECOD
---------
different




DB2



db2 => select case when 'a' = 'a ' then 'same' else 'different' end as test from sysibm.sysdummy1

TEST
---------
same

1 record(s) selected.

db2 => select 'a' from sysibm.sysdummy1 minus select 'a ' from sysibm.sysdummy1

1
--

0 record(s) selected.



Postgres



postgres=# select case when 'a' = 'a ' then 'same' else 'different' end as test ;
test
-----------
different
(1 row)

postgres=# select 'a' as a except select 'a ' as a;
a
---
a
(1 row)



SQL Server



1> select case when 'a' = 'a ' then 'same' else 'different' end as test ;
2> go
test
----
same

(1 rows affected)
1>
2> select 'a'
3> except
4> select 'a '
5> go

--

(0 rows affected)

2010-01-25

select into

How come lines 9 and 10 do not execute in the following is_active function? Documentation says that use into_clause only when there is one row in the result. Should the remaining code be ignored when there are no rows. Be sure to have exactly that one row available when using select into structure. Count(*) is a way here to be sure there is the one row.



SQL> CREATE OR REPLACE function is_active(i_is_active number)
2 return number as
3 ret number := 0;
4 begin
5 dbms_output.put_line('-'||ret||'-before-'||i_is_active||'-');
6 select 1 into ret
7 from dual
8 where i_is_active = 1;
9 dbms_output.put_line('-'||ret||'-after -'||i_is_active||'-');
10 if ret is null then ret := 0;
11 end if;
12 return ret;
13 end;
14 /

Function created.

SQL>
SQL> select is_active(1),is_active(0) from dual
2 ;

IS_ACTIVE(1) IS_ACTIVE(0)
------------ ------------
1

-0-before-1-
-1-after -1-
-0-before-0-
SQL>
SQL>
SQL> CREATE OR REPLACE function is_active_usingcount(i_is_active number)
2 return number as
3 ret number := 0;
4 begin
5 dbms_output.put_line('-'||ret||'-before-'||i_is_active||'-');
6 select count(*) into ret
7 from dual
8 where i_is_active = 1;
9 dbms_output.put_line('-'||ret||'-after -'||i_is_active||'-');
10 return ret;
11 end;
12 /

Function created.

SQL>
SQL> select is_active_usingcount(1),is_active_usingcount(0) from dual
2 ;

IS_ACTIVE_USINGCOUNT(1) IS_ACTIVE_USINGCOUNT(0)
----------------------- -----------------------
1 0

-0-before-1-
-1-after -1-
-0-before-0-
-0-after -0-



You get "ORA-01422: exact fetch returns more than requested number of rows" if there are more than one row. Should there be an exception also to the case when there are no rows?

Update 4.11.2010:
Well there is a exception thrown. Just not catching it anywhere.

CREATE OR REPLACE function is_active(i_is_active number)
return number as
ret number := 0;
begin
dbms_output.put_line('-'||ret||'-before-'||i_is_active||'-');
select 1 into ret
from dual
where i_is_active = 1;
dbms_output.put_line('-'||ret||'-after -'||i_is_active||'-');
if ret is null then ret := 0;
end if;
return ret;
EXCEPTION
WHEN NO_DATA_FOUND THEN return -1;
end;
/

select is_active(1),is_active(0) from dual;

IS_ACTIVE(1) IS_ACTIVE(0)
------------ ------------
1 -1

About Me

My photo
I am Timo Raitalaakso. I have been working since 2001 at Solita Oy as a Senior Database Specialist. My main focus is on projects involving Oracle database. Oracle ACE alumni 2012-2018. In this Rafu on db blog I write some interesting issues that evolves from my interaction with databases. Mainly Oracle.