Solved

Pivoting in SQL using the 10g

Posted on 2011-03-03
13
502 Views
Last Modified: 2012-05-11
I have a table having few data and the shift details for one month.
For the whole month the shift are going to be same so now I want to know on what date is a EMP is in which shift.

My source table structure is :

 
CREATE TABLE SHIFT_SUMMARY_NEW
(
SRNO NUMBER,
YEAR NUMBER(4),
MONTHS VARCHAR2(3),
DBA_NAME VARCHAR2(50),
SUN VARCHAR2(50),
MON VARCHAR2(50),
TUE VARCHAR2(50),
WED VARCHAR2(50),
THU VARCHAR2(50),
FRI VARCHAR2(50),
SAT VARCHAR2(50)
);


CREATE SEQUENCE SHIFT_SUMMARY_NEW_SEQ MINVALUE 1 START WITH 1 INCREMENT BY 1 CACHE 20;

 CREATE OR REPLACE TRIGGER "MGMT"."SHIFT_MASTER_NEW_BR_I" 
  before insert on "SHIFT_SUMMARY_NEW"               
  for each row  
begin   
  if :NEW."SRNO" is null then 
    select "SHIFT_SUMMARY_NEW_SEQ".nextval into :NEW."SRNO" from dual; 
  end if; 
end; 
/
ALTER TRIGGER "MGMT"."SHIFT_MASTER_NEW_BR_I" ENABLE;

Open in new window



Now i have two records in my source table like as below:

YEAR      MONTHS      EMP      SUN      MON      TUE      WED      THU      FRI      SAT
2011      MAR      PINKURAY      WEEK OFF      WEEK OFF      IST      PST      EST      EST      IST


Attached the file for the source and expected result set.

DATAFILE.xls
0
Comment
Question by:pinkuray
  • 7
  • 6
13 Comments
 
LVL 4

Author Comment

by:pinkuray
Comment Utility
I have only one record in this.
Now I want to pull the records in such away that I will get to know the shift details of a emp.

The shift will be fixed for the whole month.

For example if a EMP of in IST shift on MONDAY then he will be coming in IST for all mondays on that month.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
try this. It works fine for me.

select to_char(to_date(months || '-' || year,'MON-YYYY') + (level-1) , 'DY') Day,
to_date(months || '-' || year,'MON-YYYY') + (level-1) work_date,
 dba_name,
 decode(to_char(to_date(months || '-' || year,'MON-YYYY') + (level-1) , 'DY'),
 'SUN',SUN,
 'MON',MON,
 'TUE',TUE,
 'WED',WED,
 'THU',THU,
 'FRI',FRI,
 'SAT',SAT) shift_type  
from dual , shift_summary_new
connect by level <= ( select to_number(to_char(last_day(to_date(months || '-' || year,'MON-YYYY')),'DD')) from shift_summary_new )
0
 
LVL 4

Author Comment

by:pinkuray
Comment Utility
its giving me errror when I run your query:

ORA-01427: single-row subquery returns more than one row

0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
how many records are there in your shift summary new table ?
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
see the query output which i had given in the attached document.
ee.doc
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
you should have only one record in the shift_summary_new table otherwise it will give that error.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 500 total points
Comment Utility
if you have more than one record in the table, but you want to get the output for one particular DBA, then use this query.

I have tested this and it works fine for me. You need to change PINKURAY to some other DBA name if you want to run the query for that DBA.

select to_char(to_date(months || '-' || year,'MON-YYYY') + (level-1) , 'DY') Day,
to_date(months || '-' || year,'MON-YYYY') + (level-1) work_date,
 dba_name,
 decode(to_char(to_date(months || '-' || year,'MON-YYYY') + (level-1) , 'DY'),
 'SUN',SUN,
 'MON',MON,
 'TUE',TUE,
 'WED',WED,
 'THU',THU,
 'FRI',FRI,
 'SAT',SAT) shift_type  
from dual , ( select * from shift_summary_new where dba_name ='PINKURAY')
connect by level <= ( select to_number(to_char(last_day(to_date(months || '-' || year,'MON-YYYY')),'DD')) from shift_summary_new
where dba_name ='PINKURAY' )
0
 
LVL 4

Author Comment

by:pinkuray
Comment Utility
YES true.

A quick question can we write some trigger on the mail table like ..
is any record is inserted into shift_summary_new  table then it will insert all the 31 or monthly records into another table which is as similar to this result ?
0
 
LVL 4

Author Comment

by:pinkuray
Comment Utility
I need to insert more records in shift_summary_new  table where it will help me to insert into the 2nd result table.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
please consider that as a new question and i will post my update shortly as i do not want to discuss regarding that in this question.

0
 
LVL 4

Author Comment

by:pinkuray
Comment Utility
0
 
LVL 4

Author Closing Comment

by:pinkuray
Comment Utility
Thanks a lot
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
Good and thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now