Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I want is to pass a complete range of dates in the Pivot "IN" clause. But what i am doing is that using the values only that i am getting from database.

For ex.

Suppose if user select the From date as '10/10/2015' and To date as '10/15/2015' then I want to use all the values (10/10/2015,10/11/2015,10/12/2015,10/13/2015,10/14/2015,10/15/2015)

But what is happening from my query is ('10/10/2015','10/15'2015')

Now the data is coming like this:
M_NAME PHONE JOB_ID Assigned_DATE SHIFT Assignment E_ID EMP_NAME DEPT_COLOR '10/10/2015' '10/15/2015'

But I want like this:
M_NAME PHONE JOB_ID Assigned_DATE SHIFT Assignment E_ID EMP_NAME DEPT_COLOR '10/10/2015' '10/11/2015' '10/12/2015' '10/13/2015' '10/14/2015' '10/15/2015'


What I have tried:

SQL
SELECT * FROM 
             (
 SELECT 
  Employee.M_NAME AS MANAGER_NAME,
  Employee.PHONE,
  Employee.JOB_ID,
  Employee.Assigned_DATE,
  Employee.SHIFT,
  Employee.Dept as Assignment, 
  Employee.E_ID, 
  Employee.NAME AS EMP_NAME,
  Employee.DEPT_COLOR
  FROM  Employee
  WHERE Assigned_Date BETWEEN
  TO_DATE('FD_Selected','MM/DD/YYYY') AND
  TO_DATE('TD_Selected','MM/DD/YYYY')
  ORDER by Employee.E_ID
             ) x
             PIVOT 
             (
                 min(Assignment)
                 FOR Assigned_Date IN (TO_DATE('FD_Selected','YYYY-MM-DD'),
                                      TO_DATE('TD_Selected','YYYY-MM-DD')
              )
          ) p 
Posted
Updated 7-Jan-17 1:15am
v4

1 solution

As far as I know you cannot define the PIVOT clause to use dynamic amount of columns. You need to define each returned column explicitly. So in your example the SQL should look like
SQL
SELECT * FROM (
 SELECT 
  Employee.M_NAME AS MANAGER_NAME,
  Employee.PHONE,
  Employee.JOB_ID,
  Employee.Assigned_DATE,
  Employee.SHIFT,
  Employee.Dept as Assignment, 
  Employee.E_ID, 
  Employee.NAME AS EMP_NAME,
  Employee.DEPT_COLOR
  FROM  Employee
  WHERE Assigned_Date BETWEEN TO_DATE('FD_Selected','MM/DD/YYYY') 
                      AND TO_DATE('TD_Selected','MM/DD/YYYY')
  ORDER by Employee.E_ID
  ) x
  PIVOT (
     min(Assignment)
     FOR Assigned_Date IN (TO_DATE('10/10/2015', 'YYYY-MM-DD'),
                           TO_DATE('10/11/2015', 'YYYY-MM-DD'),
                           TO_DATE('10/12/2015', 'YYYY-MM-DD'),
                           TO_DATE('10/13/2015', 'YYYY-MM-DD'),
                           TO_DATE('10/14/2015', 'YYYY-MM-DD'),
                           TO_DATE('10/15/2015', 'YYYY-MM-DD'))
  )
) p 

In other words you need to build the dynamic portion when building the SQL statement itself.
 
Share this answer
 
Comments
Rambo_Raja 7-Jan-17 7:24am    
Thanks Mika. I am trying this by using this query which is returning me all the dates that falls in the selected date range.

select TO_DATE('11/10/2016','MM/DD/YYYY') + rownum -1
from all_objects
where rownum <= TO_DATE('12/31/2016','MM/DD/YYYY') - TO_DATE('11/10/2016','MM/DD/YYYY')+1 ;
Wendelius 7-Jan-17 7:41am    
The query returns the full date range but if you want each of the dates to appear in a separate column when pivoted then you need to have a column definition in the PIVOT clause for all dates.

In other words if you want 2 dates you define 2 columns in PIVOT FOR IN. If you want 5 days you need 5 column definitions. So column definitions are not dynamic.
Rambo_Raja 7-Jan-17 7:24am    
But the thing is that I am unable to use the output of this query. Can you please help me with that? Regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900