3

データを含む2 つのテーブルがありLeaves, Attendanceatdます。

ListofDatesこれらの 2 つのテーブルとは異なるものを表示する必要があり、これらの 2 つのテーブルから、両方のテーブルの Date 列employeeidで、Leave table approvalstatus='Approved' および startdate / enddate period 期間という条件が設定されています。

テーブル スキーマ: SQL FIDDLE

create table Leaves(idemp_lv int,leavedate Datetime, approvalstatus varchar(100))
    insert into Leaves values (1,'2013-04-22 00:00:00.000','Approved');
    insert into Leaves values (2,'2013-04-21 00:00:00.000','Approved'); 
    insert into Leaves values(3,'2013-04-26 00:00:00.000','Approved');
    insert into Leaves values(1,'2013-04-21 00:00:00.000','Pending'); 
    insert into Leaves values(3,'2013-04-02 00:00:00.000','Pending');
    insert into Leaves values(1,'2013-04-19 00:00:00.000','Approved');

create table Attendanceatd(idemp_at int,absentdate Datetime)
insert into Attendanceatd values(1,'2013-04-19 00:00:00.000');
insert into Attendanceatd values(3,'2013-04-02 00:00:00.000');
insert into Attendanceatd values(1,'2013-04-15 00:00:00.000');

望ましい出力:

Empid ListofDate
1     2013-04-22 00:00:00.000
1     2013-04-19 00:00:00.000
1     2013-04-15 00:00:00.000
2     2013-04-21 00:00:00.000
3     2013-04-26 00:00:00.000
3     2013-04-02 00:00:00.000

現在、私はこれを試しています:

select ListOfDates
from 
(
 select leavedate as ListOfDates from leaves where approvalstatus='Approved' and leavedate >='20130302' and  leavedate <'20130501'
 union
 select absentdate  as ListOfDates from Attendanceatd where  absentdate >='20130302' and  absentdate <'20130501'
)t1
group by ListOfDates 

ListOfDatesと一緒にemployeeIdを取得できません

4

3 に答える 3

1
select distinct empid, ListOfDates
from 
(
 select idemp_lv as empid, leavedate as ListOfDates from leaves where approvalstatus='Approved' and leavedate >='20130302' and  leavedate <'20130501'
 union all
 select idemp_at as empid, absentdate  as ListOfDates from Attendanceatd where  absentdate >='20130302' and  absentdate <'20130501'
)t1
group by empid, ListOfDates 

何かが欠けていない限り、トリックを行う必要があります。ユニオンオールは高速ですが、より集中的なSQLソートになることを覚えておいてください。常に実行計画にアクセスして、パフォーマンスを確認してください。

于 2013-05-27T10:26:55.853 に答える
1
select EmpId,ListOfDates
from 
(
select idemp_lv as EmpId ,leavedate as ListOfDates from leaves where approvalstatus='Approved' 
union all
select idemp_at as EmpId , absentdate  as ListOfDates from Attendanceatd
)T1
where ListOfDates between '2013-03-02' and '2013-05-01'
group by  EmpId,ListOfDates
于 2013-05-27T10:29:41.347 に答える