Solved

oracle query

Posted on 2012-03-29
9
359 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 76

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 28

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 76

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
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: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 76

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 76

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

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.

Question has a verified solution.

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

Suggested Solutions

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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Via a live example, show how to take different types of Oracle backups using RMAN.

777 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