Pivot [message #642696] |
Thu, 17 September 2015 07:59 |
|
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 #642702 is a reply to message #642701] |
Thu, 17 September 2015 08:30 |
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 |
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
|
|
|
|
|