Solved

Provide a SQL query that returns the user name and the total

Posted on 2012-04-04
6
470 Views
Last Modified: 2012-04-04
Please provide the solution for the below query :

enclosed the script file with test data.

The table  IGBU_PROJ_RESOURCE holds information about users and their
corresponding location. Various holidays declared for each location
are stored in the table IGBU_LOC_HOLIDAY. Finally, there is a table
named IGBU_PROJ_TRACKING that holds information about the period range
during which resources worked on a project. In some case, the resource
might have worked only half-day and if so, the column half_day_flag
will have the value 'Y'. Otherwise, N is specified for full-day.
Specified period is stored in columns - start_date and end_date [both
inclusive]. In the specified date range, there may be weekends
(Saturday and Sunday) as well as declared holidays for the location
the resource belongs to.
(Some of the columns in the table are removed for keeping it simple)

Problem:  Provide a SQL query that returns the user name and the total
number of days worked on the project during the period 01-MAR-2012 and
31-MAR-2012 (both dates inclusive). Total number of days must exclude
declared holidays and weekends.

Expected Query Result
user_name            total_days
abc.user@oracle.com  6.5
mno.user@oracle.com  6.0
pqr.user@oracle.com  7.0
xyz.user@oracle.com  7.0
problem.sql
0
Comment
Question by:vickyjava
  • 2
  • 2
6 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 37805868
try this...

SELECT user_name,
       SUM(
             (tracked_days - holiday_cnt - weekend_cnt)
           * CASE WHEN half_day_flag = 'Y' THEN .5 ELSE 1 END
       )
           total_days
  FROM (SELECT user_name,
               (modified_end - modified_start) + 1 tracked_days,
               half_day_flag,
               (SELECT COUNT(
                           CASE WHEN TO_CHAR(holiday_date, 'Dy') NOT IN ('Sat', 'Sun') THEN 1 END
                       )
                  FROM igbu_loc_holiday h
                 WHERE x.location_id = h.location_id
                   AND h.holiday_date BETWEEN modified_start AND modified_end)
                   holiday_cnt,
               (SELECT COUNT(
                           CASE
                               WHEN TO_CHAR(modified_start + LEVEL - 1, 'Dy') IN ('Sat', 'Sun')
                               THEN
                                   1
                           END
                       )
                  FROM DUAL
                CONNECT BY modified_start + LEVEL - 1 <= modified_end)
                   weekend_cnt
          FROM (SELECT r.user_name,
                       GREATEST(t.start_date, i.range_start) modified_start,
                       LEAST(t.end_date, i.range_end) modified_end,
                       r.location_id,
                       t.half_day_flag
                  FROM igbu_proj_resource r,
                       igbu_proj_tracking t,
                       (SELECT DATE '2012-03-01' range_start, DATE '2012-03-31' range_end FROM DUAL) i
                 WHERE r.user_id = t.user_id
                   AND t.start_date <= i.range_end
                   AND t.end_date >= i.range_start) x)
GROUP BY user_name
ORDER BY user_name

Open in new window




I added checks to make sure your tracking ranges didn't extend beyond the March 1-31 input range
if that's not necessary then remove    

 GREATEST(t.start_date, i.range_start) modified_start,
 LEAST(t.end_date, i.range_end) modified_end,

and change all references of modified_start to start_date  and all references of modified_end to end_date


and I also checked for holdays that are on weekends to make sure we don't double subtract
 if that's not necessary simply  change

"CASE WHEN TO_CHAR(holiday_date, 'Dy') NOT IN ('Sat', 'Sun') THEN 1 END"
to
"*"
0
 
LVL 32

Expert Comment

by:awking00
ID: 37807720
>>Expected Query Result
user_name            total_days
abc.user@oracle.com  6.5
mno.user@oracle.com  6.0
pqr.user@oracle.com  7.0
xyz.user@oracle.com  7.0<<
Can you provide the actual table data for which those results are expected?
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37807771
he did  - it's what I tested with
0
 
LVL 32

Expert Comment

by:awking00
ID: 37807793
Sorry, I missed seeing the attachment.
0
 

Author Closing Comment

by:vickyjava
ID: 37808178
Thank you for your help its working!
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

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…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now