Solved

Pivoting in SQL using the 10g

Posted on 2011-03-03
13
505 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
ID: 35034219
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
ID: 35034250
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
ID: 35034277
its giving me errror when I run your query:

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

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

Expert Comment

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

Expert Comment

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

Expert Comment

by:Naveen Kumar
ID: 35034298
you should have only one record in the shift_summary_new table otherwise it will give that error.
0
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 500 total points
ID: 35034308
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
ID: 35034310
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
ID: 35034314
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
ID: 35034317
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
ID: 35034349
0
 
LVL 4

Author Closing Comment

by:pinkuray
ID: 35034351
Thanks a lot
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 35034428
Good and thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  â€¦
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

856 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