問題: テーブルに保存されている給与情報に日付を付けています。1 年に 1 つの結果を表示する必要があります。毎年、前年の最大日付レコードを表示したいと考えています。問題は、年によってはデータがない (給与が変わらない) ことです。これらの行には、その年より前の最大レコードが含まれている必要があります (2 年前または 3 年前の可能性もあります)。
各行にデータがある場合、私のクエリは今のところ機能しますが、データがない年は考慮されていません。このSQLを更新して、以下の目的の結果を取得するにはどうすればよいですか:
データの例:
sch_sal_svc.beg_date -------sch_sal_svc.beg_date.per_plan_data
- 2007/1/1---100
- 2007/6/1---200
- 2008/1/1---300
- 2011/1/1---400
- 2011/8/1---500
- 2012/9/1---600
現在の結果
- 2008/1/1---200
- 2011/1/1---300
- 2012/1/1---500
望ましい結果
- 2008/1/1---200
- 2009/1/1---300
- 2010/1/1---300
- 2011/1/1---300
- 2012/1/1---500
SQL:
SELECT
years.control_id,
years.ssn,
ebe.plan_id,
to_number(to_char(years.sal_date,'yyyy')),
null as per_plan_salary,
null as per_vest_hours,
null as per_credsvc_hours,
LEAST(s.rate_1,cl.comp_genl),
null as salary_1,
null as per_comm,
null as per_overtime,
null as per_ncr,
null as salary_2
FROM
sch_sal_svc s
, (select distinct ssn, control_id, TRUNC(beg_date,'YEAR') as sal_date from sch_sal_svc where beg_date > to_date('12/31/1900', 'mm/dd/yyyy')) years
, employee_benefit_elig ebe, compliance_limits cl
WHERE
years.ssn = ebe.ssn
and years.control_id = ebe.control_id
and to_number(to_char(years.sal_date,'yyyy')) = cl.limit_year
and to_number(to_char(years.sal_date,'yyyy')) <= to_number(to_char(sysdate,'yyyy'))
and s.beg_date = (
select max(s2.beg_date) from sch_sal_svc s2
where s2.ssn = years.ssn and s2.control_id = years.control_id
and s2.beg_date <= years.sal_date
)
and s.ssn = years.ssn
and s.control_id = years.control_id
and ebe.benefit_id = 'DB'
and ebe.control_id = 'CLIENT'
and ebe.plan_id in ('100', '200')