sysdate in the pivot in clause [message #651509] |
Thu, 19 May 2016 05:58 |
pointers
Messages: 451 Registered: May 2008
|
Senior Member |
|
|
Hi,
create table t_pivot (id number, l_id number, l_date date);
begin
insert into t_pivot values (1, 10, sysdate);
insert into t_pivot values (2, 20, sysdate-2);
insert into t_pivot values (3, 20, sysdate-2);
insert into t_pivot values (4, 2, sysdate+1);
insert into t_pivot values (5, 10, sysdate+1);
end;
/
commit;
I would like to use sysdate in the "pivot IN" clause, it throws me an error.
ORA-56901: non-constant expression is not allowed for pivot|unpivot values
My query is -
select *
from (select id,l_id, l_date
from t_pivot)
pivot ( sum(l_id) for l_date in ( TRUNC(sysdate) aa
, TRUNC(sysdate-2) bb
, TRUNC(sysdate+2) cc
)
)
I could write something like below,but I would like to have the dates based on sysdate to make dynamic.
select *
from (select id,l_id, l_date
from t_pivot)
pivot ( sum(l_id) for l_date in (to_date('19-05-2016','dd-mm-yyyy') aa
, to_date('17-05-2016','dd-mm-yyyy') bb
, to_date('20-05-2016','dd-mm-yyyy') cc
)
)
Thank you in advance.
Regards,
Pointers
|
|
|
|
|
|
|
Re: sysdate in the pivot in clause [message #651527 is a reply to message #651526] |
Thu, 19 May 2016 09:50 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
SQL> col c1 new_value c1
SQL> col c2 new_value c2
SQL> col c3 new_value c3
SQL> select to_char(sysdate,'DD-MM-YYYY') c1,
2 to_char(sysdate-2,'DD-MM-YYYY') c2,
3 to_char(sysdate+1,'DD-MM-YYYY') c3
4 from dual
5 /
C1 C2 C3
---------- ---------- ----------
19-05-2016 17-05-2016 20-05-2016
1 row selected.
SQL> select *
2 from (select id,l_id, trunc(l_date) l_date from t_pivot)
3 pivot ( sum(l_id) for l_date in (to_date('&c1','dd-mm-yyyy') aa
4 , to_date('&c2','dd-mm-yyyy') bb
5 , to_date('&c3','dd-mm-yyyy') cc
6 )
7 )
8 /
ID AA BB CC
---------- ---------- ---------- ----------
1 10
2 20
4 2
5 10
3 20
5 rows selected.
|
|
|
|