Home » SQL & PL/SQL » SQL & PL/SQL » sysdate in the pivot in clause (Oracle 11.2.0.3)
sysdate in the pivot in clause [message #651509] Thu, 19 May 2016 05:58 Go to next message
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 #651512 is a reply to message #651509] Thu, 19 May 2016 06:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You can't, this syntax allows only constant.
Use the old pivot way to do it (SUM(DECODE...
And remember that SYSDATE contains a time part.

Re: sysdate in the pivot in clause [message #651524 is a reply to message #651512] Thu, 19 May 2016 09:04 Go to previous messageGo to next message
pointers
Messages: 451
Registered: May 2008
Senior Member
Thank you Micheal.

So I understand that there is no alternate way using the pivot approach. only way is to use sum(decode(.. approach

Regards,
Pointers
Re: sysdate in the pivot in clause [message #651525 is a reply to message #651524] Thu, 19 May 2016 09:40 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If you use SQL*Plus you can use a SQL*Plus trick but it is no more SQL.

Re: sysdate in the pivot in clause [message #651526 is a reply to message #651525] Thu, 19 May 2016 09:43 Go to previous messageGo to next message
pointers
Messages: 451
Registered: May 2008
Senior Member
Can you please show us how to do it in sql*plus.
That would be new learning for me.

Regards,
Pointers
Re: sysdate in the pivot in clause [message #651527 is a reply to message #651526] Thu, 19 May 2016 09:50 Go to previous messageGo to next message
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.

Re: sysdate in the pivot in clause [message #651565 is a reply to message #651527] Fri, 20 May 2016 02:01 Go to previous message
pointers
Messages: 451
Registered: May 2008
Senior Member
Thank you Micheal.

Regards,
Pointers
Previous Topic: Transpose Columns to Row group by
Next Topic: Pattern Matching for Multivalue
Goto Forum:
  


Current Time: Fri Apr 26 12:01:13 CDT 2024