Link to home
Start Free TrialLog in
Avatar of pinkuray
pinkurayFlag for India

asked on

SQL Ouery in Oracle

Hello,

I have a table with few data, I want to have a report from that table as like below:

WORK_DATE      DBA      WORK_STATUS      SHIFT_TYPE
Tuesday, March 01, 2011      JOHN      Yes      Morning
Tuesday, March 01, 2011      PINKURAY      Weekoff      Null
Tuesday, March 01, 2011      MARK      Yes      Morning
Wednesday, March 02, 2011      JOHN      Yes      Morning
Wednesday, March 02, 2011      PINKURAY      Yes      Afternoon
Wednesday, March 02, 2011      MARK      Yes      Afternoon
Thursday, March 03, 2011      JOHN      Yes      Morning
Thursday, March 03, 2011      PINKURAY      Yes      Afternoon
Thursday, March 03, 2011      MARK      Yes      Afternoon
Friday, March 04, 2011      JOHN      Yes      Morning
Friday, March 04, 2011      PINKURAY      Yes      Afternoon
Friday, March 04, 2011      MARK      Yes      Afternoon
Saturday, March 05, 2011      JOHN      Weekoff      Null
Saturday, March 05, 2011      PINKURAY      Yes      Afternoon
Saturday, March 05, 2011      MARK      Weekoff      Null
Sunday, March 06, 2011      JOHN      Weekoff      Null
Sunday, March 06, 2011      PINKURAY      Yes      Afternoon
Sunday, March 06, 2011      MARK      Weekoff      Null
Monday, March 07, 2011      JOHN      Yes      Morning
Monday, March 07, 2011      PINKURAY      Weekoff      Null
Monday, March 07, 2011      MARK      Yes      Morning


The Source table having the data is been attached here and the structure of the table is like below:

 
/* 
W_DAY : "W" STANDS FOR THE WORKING , WHERE EVER THE W_ COLUMN WILL BE FILLED AS YES OR WEEKOFF
S_DAY : "S" STANDS FOR SHIFT, IT CAN BE Morning OR Afternoon OR Night OR General
*/

CREATE TABLE  "SHIFT_SUMMARY" 
   ( "SRNO" NUMBER, 
 "DBA" VARCHAR2(10), 
 "W_MON" VARCHAR2(50), 
 "S_MON" VARCHAR2(50), 
 "W_TUE" VARCHAR2(50), 
 "S_TUE" VARCHAR2(50), 
 "W_WED" VARCHAR2(50), 
 "S_WED" VARCHAR2(50), 
 "W_THU" VARCHAR2(50), 
 "S_THU" VARCHAR2(50), 
 "W_FRI" VARCHAR2(50), 
 "S_FRI" VARCHAR2(50), 
 "W_SAT" VARCHAR2(50), 
 "S_SAT" VARCHAR2(50), 
 "W_SUN" VARCHAR2(50), 
 "S_SUN" VARCHAR2(50), 
 "MONTH" VARCHAR2(50), 
 "YEAR" NUMBER, 
  CONSTRAINT "SHIFT_SUMMARY_PK" PRIMARY KEY ("SRNO") ENABLE
   )
/

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

CREATE OR REPLACE TRIGGER  "SHIFT_SUMMARY_BR_I" 
  before insert on "SHIFT_SUMMARY"               
  for each row  
begin   
  if :NEW."SRNO" is null then 
    select "SHIFT_SUMMARY_SEQ".nextval into :NEW."SRNO" from dual; 
  end if; 
end; 

/
ALTER TRIGGER  "SHIFT_SUMMARY_BR_I" ENABLE
/

Open in new window


The expected result is  also attached here.

SAMPLE-DATA.xls
SOLUTION
Avatar of Aaron Shilo
Aaron Shilo
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pinkuray

ASKER

I tried using UNPIVOT this query but not working for me

I used the query like below for getting working days as :

 
SELECT *
FROM   SHIFT_SUMMARY
UNPIVOT INCLUDE NULLS 
(WORKINGDAYS FOR WORKINGWEEKS 
IN (W_MON AS 'WORKING_ON_MONDAY', W_TUE AS 'WORKING_ON_TUE', W_WED AS 'WORKING_ON_WED', W_THU AS 'WORKING_ON_THU' ,W_FRI AS 'WORKING_ON_FRI',W_SAT AS 'WORKING_ON_SAT',W_SUN AS 'WORKING_ON_SUN'));

Open in new window


Error:
ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"

Even if run the queries that are given on the link is also not working.

Can you please guide me what is wrong in this?
the expected output for "Work_Date" column shows march 1st to 6th onwards, but this date itself is not there in your table data. So how do you get this ?

Also i believe PIVOT is best option to use here if you are on oracle 11g.

Thanks
What is your Oracle version?
My database version is :

Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
cant we use the PIVOT and UNPIVOT Operators in Oracle Database 10G  ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
based on this article we can use the same functionality in 10g too:

http://www.bobjankovsky.org/show.php?seq=97

When I tried I got the same error as I mentioned before.
can you send me the code for my tables to get the expected result?
@pinkuray,

You should have told about your calendar table/join idea before itself.

just give the table script / sample data for your calendar table for me to setup that in my test database.

But i believe it is not an easy query to do without using the PIVOT. but 10g does not have PIVOT/UNPIVOT etc.

No I just thought of having a calendar table which may help me to do this. As I don't have any table for calendar but when you try to use a calender you will see similar way I have designed the table structure.

So thought of having a table or just going thought it will help.

I want a solution for my issue, even having PIVOT or not.

Can you please help me in this.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have posted another question related to this can you help me in that ...
questions is on " Pivoting in SQL using the 10g"
it is similar to this question only difference is shift are fixed for the whole month.
what is the question link to that one ?
Accepting this point s as my logic was not up to the mark but all your post where very helpful to build my new logic thanks a lot