I am trying to transpose columns to rows using query similar to the following…
WITH
query AS
(
SELECT SYSDATE AS SomeDate,
'One' AS One,
'Two' AS Two,
'Three' AS Three,
'Four' AS Four,
'Five' AS Five
FROM dual
),
up_query AS
(
SELECT *
FROM query
UNPIVOT
(
NUM FOR DUMMY
IN
(
One AS 'One',
Two AS 'Two',
Three AS 'Three',
Four AS 'Four',
Five AS 'Five'
)
)
)
SELECT SYSDATE, b.*
FROM up_query b;
I was expecting SomeDate to reflect SYSDATE for the resulting rows…
But this is the result I am getting:
SYSDATE SOMEDATE DUMMY NUM
09-DEC-11 09-DEC-07 One One
09-DEC-11 09-DEC-07 Two Two
09-DEC-11 09-DEC-07 Three Three
09-DEC-11 09-DEC-07 Four Four
09-DEC-11 09-DEC-07 Five Five
Why is the SOMEDATE 4 years earlier than SYSDATE?
As Mark mentioned in his answer, this is a bug in Oracle 11.2.0.1 and 11.2.0.2 versions atleast.
However as per this article there is a workaround if you are stuck with the Oracle versions mentioned above, which is to convert the date to varchar format and then convert it back to date datatype.
So the query should now be: