Home » SQL & PL/SQL » SQL & PL/SQL » Pivot
icon5.gif  Pivot [message #642696] Thu, 17 September 2015 07:59 Go to next message
tunny2014
Messages: 3
Registered: September 2015
Junior Member
Afternoon,

I'm trying to pivot some user data but I'm really struggling, can any one look to see what I'm doing wrong?

with pivot_data as
  (
    select user_id, EXTRACT(hour from dstamp) as somehour, sum(update_qty) as someqty
    from inventory_transaction
    where TO_CHAR(dstamp,'DD/MM/YYYY hh:mm') between '16/09/2015 06:00' and '17/09/2015 05:59'
    group by user_id, EXTRACT(hour from dstamp)
    Order By EXTRACT(hour from dstamp)
  )
select * 
from pivot_data
  PIVOT (
          sum(update_qty)        --<-- pivot_clause
          FOR EXTRACT(hour from dstamp)        --<-- pivot_for_clause
          IN  (11,12,13,14)   --<-- pivot_in_clause
         );


Thanks Andy
Re: Pivot [message #642697 is a reply to message #642696] Thu, 17 September 2015 08:11 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

We don't have your tables or data.
It is hard to say what you are doing wrong when we don't know for sure what the results should be to be considered correct or right.
Re: Pivot [message #642698 is a reply to message #642697] Thu, 17 September 2015 08:13 Go to previous messageGo to next message
tunny2014
Messages: 3
Registered: September 2015
Junior Member
Would of helped if I had actually put the error message in the post.....

ORA-01738: missing IN keyword
01738. 00000 - "missing IN keyword"
Re: Pivot [message #642699 is a reply to message #642698] Thu, 17 September 2015 08:16 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
What's your oracle version?
Re: Pivot [message #642701 is a reply to message #642699] Thu, 17 September 2015 08:25 Go to previous messageGo to next message
tunny2014
Messages: 3
Registered: September 2015
Junior Member
Oracle Database 12c Release 12.1.0.1.0 - 64bit Production
Re: Pivot [message #642702 is a reply to message #642701] Thu, 17 September 2015 08:30 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
First thing I'd point out is that the column names you're referring to in the pivot clause don't exist because you've aliased them to other names in the with clause.
Re: Pivot [message #642703 is a reply to message #642702] Thu, 17 September 2015 08:36 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Also your where clause is wrong - dates don't sort the same way as strings so don't convert dates to strings:
SQL> SELECT 1 FROM dual
  2  WHERE TO_CHAR(to_date('16/10/2500 02:00', 'DD/MM/YYYY hh:mi'),'DD/MM/YYYY hh:mi') between '16/09/2015 06:00' and '17/09/2015 05:59';
 
         1
----------
         1

SQL> SELECT 1 FROM dual
WHERE to_date('16/10/2500 02:00', 'DD/MM/YYYY hh:mi') between to_date('16/09/2015 06:00','DD/MM/YYYY hh:mi') and to_date('17/09/2015 05:59','DD/MM/YYYY hh:mi');  2 

no rows selected



Also the format mask for minutes is mi not mm
Re: Pivot [message #642705 is a reply to message #642703] Thu, 17 September 2015 08:44 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That order by is a waste of time
Re: Pivot [message #642710 is a reply to message #642696] Thu, 17 September 2015 09:03 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please post CREATE TABLE inventory_transaction statement
Previous Topic: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2]
Next Topic: SQL to get amounts by type
Goto Forum:
  


Current Time: Thu Apr 18 19:55:41 CDT 2024