Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

oracle query

Posted on 2012-03-29
9
Medium Priority
?
366 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 1000 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 78

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 78

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 78

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

916 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