Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi , I have a doubt in SQL . Supp

Status
Not open for further replies.

uparna

Technical User
Jun 27, 2001
182
IN
Hi ,
I have a doubt in SQL . Suppose i have 2 tables

table 1 : employee_id
time
effort_date
week_ending

table 2 : employee_id
authorization
week_ending

and i have a query that runs like this :

select
emp_id , sum(time), effort_date , authorization
from
table1 a , table2 b
where
a.employee_id = b.employee_id and
a.week_ending = b.week_ending and
a.employee_id = '007' and
a.effort_date between
to_date('01/09/2001','dd/mm/yyyy') and
to_date('30/09/2001','dd/mm/yyyy')
group by
emp_id , effort_date , authorization ;


this returns the following data :

007 9 24/09/2001 30/09/2001
007 14 27/09/2001 30/09/2001


but i want the data in this fashion for the month of september

007 0 01/09/2001 02/09/2001
007 0 02/09/2001 09/09/2001
007 0 03/09/2001 09/09/2001
007 0 04/09/2001 09/09/2001
007 0 05/09/2001 09/09/2001
007 0 06/09/2001 09/09/2001
007 0 07/09/2001 09/09/2001
007 0 08/09/2001 09/09/2001
.
.
.
.
.
007 0 23/09/2001 30/09/2001
007 9 24/09/2001 30/09/2001
.
.
.
007 14 27/09/2001 30/09/2001
.
.
.
007 0 30/09/2001 30/09/2001



how do i achieve this ? There is no problem with the week endings , as my table 2 has all the weekendings in a year.



Thanks in advance !

Regards,
S. Jayaram Uparna .

If the need arises,you are welcome to mail me at oracguru@yahoo.com .
:)
 
A union comes to mind:
Code:
select 
emp_id , sum(time), effort_date , authorization 
from  
table1  a , table2 b 
where
    a.employee_id = b.employee_id and 
    a.week_ending = b.week_ending and 
    a.employee_id = '007' and
    a.effort_date between 
to_date('01/09/2001','dd/mm/yyyy') and 
to_date('30/09/2001','dd/mm/yyyy')
    group by 
    emp_id , effort_date , authorization

UNION

select 
emp_id, 0 as sum(time), d.my_date as effort_date, authorization
from
table1 a, table2 b, table_dates d
where
    a.employee_id = b.employee_id and
    a.week_ending = b.week_ending and
    a.employee_id = '007' and
    d.my_date between
      to_date('01/09/2001','dd/mm/yyyy') and 
      to_date('30/09/2001','dd/mm/yyyy') and
    d.my_date < b.authorization and
    not exists(
       select 
       emp_id , sum(time), effort_date , authorization 
       from  
       table1  a2 , table2 b2
       where
         a2.employee_id = b2.employee_id and 
         a2.week_ending = b2.week_ending and 
         a2.employee_id = '007' and
         a2.effort_date = d.my_date and
         a2.effort_date between 
           to_date('01/09/2001','dd/mm/yyyy') and 
           to_date('30/09/2001','dd/mm/yyyy')
         group by 
         emp_id , effort_date , authorization
       );

being table_dates a table with all dates in a year.
 
Oh, and another thing, if you want the data ordered, you should write:
Code:
select *
from (mega-query here)
order by (you order criteria);
since unions don't accept order by clauses.
 
Unless you list the columns in the order you want them sorted. Oracle does a sort in the course of a UNION. If you want to avoid the sort, then do a UNION ALL (although this will result in Oracle removing duplicate rows).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top