Solved

oracle query

Posted on 2012-03-29
9
363 Views
Last Modified: 2012-03-30
select mail_last_name,first_name,ds.position,reason_code,schedule_date
from dept_staff ds, emp_unexcld eu
where ds.payroll_id = eu.payroll_id
and ds.schedule_date between '25-mar-2012' and '31-mar-2012'
and ds.payroll_id = 'ZZW0024543'
--------------------------------------
MAIL_LAST_NAME|FIRST_NAME|POSITION|REASON_CODE|SCHEDULE_DATE
Aguirre                     Debra           PHMB                OD                 3/25/2012
Aguirre                     Debra           PHMB                 SH                  3/26/2012
Aguirre                     Debra           PHMB                 SH                  3/28/2012
Aguirre                     Debra           PHMB                 SH                  3/29/2012
Aguirre                     Debra           PHMB                 OD                 3/30/2012
Aguirre                     Debra           PHMB                  SH                  3/31/2012
----------------------------
But I need the report in this format
Name                 Position              SUN   MON   TUE   WED     THU     FRI     SAT
Aguirre,Debra      PHMB                 OD     SH               SH        SH      OD      SH

3/25/2012 is sunday
0
Comment
Question by:anumoses
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 250 total points
ID: 37783875
Try this:

select new_name, position, max(SUN) sun, max(MON) mon, max(TUE) tue, max(WED) ewd, max(THU) thu, max(FRI) fri, max(SAT) sat from(
select mail_last_name || ',' || first_name new_name,ds.position,reason_code
            case when to_char(schedule_date,'DY')='SUN' then reason_code end SUN,
            case when to_char(schedule_date,'DY')='MON' then reason_code end MON,
            case when to_char(schedule_date,'DY')='TUE' then reason_code end TUE,
            case when to_char(schedule_date,'DY')='WED' then reason_code end WED,
            case when to_char(schedule_date,'DY')='THU' then reason_code end THU,
            case when to_char(schedule_date,'DY')='FRI' then reason_code end FRI,
            case when to_char(schedule_date,'DY')='SAT' then reason_code end SAT
from dept_staff ds, emp_unexcld eu
where ds.payroll_id = eu.payroll_id
and ds.schedule_date between '25-mar-2012' and '31-mar-2012'
and ds.payroll_id = 'ZZW0024543'
)
group by new_name, position
/
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 37783905
try this too:

select (mail_last_name ||","||first_name) as Name,ds.position,
decode(reason_code reason_code,OD,dte,null) SUN,
decode(reason_code reason_code,SH,dte,null) MON,
decode(reason_code reason_code,null,dte,null) TUE,
decode(reason_code reason_code,SH,dte,null) WED,
decode(reason_code reason_code,SH,dte,null) THU,
decode(reason_code reason_code,OD,dte,null) FRI,
decode(reason_code reason_code,SH,dte,null) SAT
from( select schedule_date as dte from 
from dept_staff ds, emp_unexcld eu
where ds.payroll_id = eu.payroll_id
and ds.schedule_date between '25-mar-2012' and '31-mar-2012'
and ds.payroll_id = 'ZZW0024543')
group by name, position

Open in new window

0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37783926
>>decode(reason_code reason_code,OD,dte,null) SUN,

Syntax is wrong.  Also the logic.  They want to return the reason_code when the date is 'SUN', etc...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:sammySeltzer
ID: 37783998
Unfortunately, I have no way of testing oracle right now but I think you are right; DECODE probably takes 3 arguments instead of 4.

As for what the user needs, I am sure he can decide that for himself.

If I am provided the wrong solution, i will apologize for it.
0
 
LVL 6

Author Comment

by:anumoses
ID: 37784020
slightwv,

Can we add order by
department_id fron dept_staff,
position,
new name,payroll_id from emp
mail last name and first name
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37784090
>>Can we add order by

You can add whatever order by you want.  Just add the order to the original select you posted.
0
 
LVL 6

Author Comment

by:anumoses
ID: 37784101
select payroll_id,
       department_id,
       new_name,
       position,
         max(SUN) sun,
         max(MON) mon,
         max(TUE) tue,
         max(WED) wed,
         max(THU) thu,
         max(FRI) fri,
         max(SAT) sat
 from(
select   eu.payroll_id,ds.department_id,first_name || ',' || mail_last_name new_name,ds.position,reason_code,
            case when to_char(schedule_date,'DY')='SUN' then reason_code end SUN,
            case when to_char(schedule_date,'DY')='MON' then reason_code end MON,
            case when to_char(schedule_date,'DY')='TUE' then reason_code end TUE,
            case when to_char(schedule_date,'DY')='WED' then reason_code end WED,
            case when to_char(schedule_date,'DY')='THU' then reason_code end THU,
            case when to_char(schedule_date,'DY')='FRI' then reason_code end FRI,
            case when to_char(schedule_date,'DY')='SAT' then reason_code end SAT
from dept_staff ds, emp_unexcld eu , department d
where ds.payroll_id = eu.payroll_id
  and d.department_id = ds.department_id
and ds.schedule_date between '25-mar-2012' and '31-mar-2012'
and eu.termination_date is null
and d.area_cd in ('BC','IN')
--and ds.payroll_id = 'ZZW0024543'
)
group by  payroll_id,department_id,new_name, position
order by department_id,position,new_name,payroll_id
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37784120
>>order by department_id,position,new_name,payroll_id

You need to add the order by on the INNER select.

something like:


select payroll_id,
       department_id,
       new_name,
       position,
         max(SUN) sun,
         max(MON) mon,
         max(TUE) tue,
         max(WED) wed,
         max(THU) thu,
         max(FRI) fri,
         max(SAT) sat
 from(
select   eu.payroll_id,ds.department_id,first_name || ',' || mail_last_name new_name,ds.position,reason_code,
            case when to_char(schedule_date,'DY')='SUN' then reason_code end SUN,
            case when to_char(schedule_date,'DY')='MON' then reason_code end MON,
            case when to_char(schedule_date,'DY')='TUE' then reason_code end TUE,
            case when to_char(schedule_date,'DY')='WED' then reason_code end WED,
            case when to_char(schedule_date,'DY')='THU' then reason_code end THU,
            case when to_char(schedule_date,'DY')='FRI' then reason_code end FRI,
            case when to_char(schedule_date,'DY')='SAT' then reason_code end SAT
from dept_staff ds, emp_unexcld eu , department d
where ds.payroll_id = eu.payroll_id
  and d.department_id = ds.department_id
and ds.schedule_date between '25-mar-2012' and '31-mar-2012'
and eu.termination_date is null
and d.area_cd in ('BC','IN')
--and ds.payroll_id = 'ZZW0024543'
order by department_id,position,first_name || ',' || mail_last_name ,payroll_id
)
group by  payroll_id,department_id,new_name, position
0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 37787377
thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

696 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