我有一个查询返回这样的查询...
资料集1:
EFFECTIVE_DATE END_DATE DESC SUBPART
4/10/2011 Dairy Products Processing L
4/10/2011 360 CMR 10.000
4/1/2011 4/9/2011 Dairy Products Processing A
4/1/2011 4/9/2011 Ferroalloy Manufacturing A
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个查询,返回像这样的数据集...
数据集2:
EFFECTIVE_DATE END_DATE DESC SUBPART
4/10/2011 Dairy Products Processing L
360 CMR 10.000
4/1/2011 4/9/2011 Dairy Products Processing A
Ferroalloy Manufacturing A
Run Code Online (Sandbox Code Playgroud)
请注意,重复生效日期(4/10/2011-{null}和4/1/2011-4/9/2011)将被取消。
编辑1: 针对@Justin Cave的回答,
以下是我的查询与Justin Cave的模板合并的结果。它很近,但是有点关。日期和说明似乎与预期的有些混淆(数据集中的数据应类似于数据集2中的数据。我认为这可能与我的订购有关,但我不确定。
SELECT (CASE WHEN effective_date = prior_effective_date
THEN null
ELSE effective_date
END) effective_date,
(CASE WHEN end_date = prior_end_date
THEN null
ELSE end_date
END) end_date,
cfr_part_desc ,
cfr_subpart
FROM
(SELECT c.effective_date,
lag(c.effective_date) over (order by c.effective_date desc, cpl.cfr_part_desc asc) prior_effective_date,
c.end_date,
lag(c.end_date) over (order by c.effective_date desc, cpl.cfr_part_desc asc) prior_end_date,
cpl.CFR_PART_DESC as cfr_part_desc,
cd.CFR_SUBPART as cfr_subpart
from table1 c
inner join table2 cd ON c.IND_ID = cd.IND_ID
AND cd.EFFECTIVE_DATE = c.EFFECTIVE_DATE
inner join table3 cpl on cd.CFR_PART_L_S = cpl.CFR_PART_L_S
inner join table4 f on c.ind_id = f.ind_id
inner join table5 p on f.ind_id = p.ind_id
where p.PERMIT_S = '4988'
order by c.effective_date desc, cpl.CFR_PART_DESC asc
);
Run Code Online (Sandbox Code Playgroud)

编辑2: 错误的数据是由于模棱两可的定义了有效日期。上面的查询现在可以正常工作。
我认为您正在使用解析函数来寻找类似的东西。我不清楚结果集的排序方式,因此您必须填写该结果集。您还可能能够将分析功能组合到查询中,而无需添加第三级嵌套。
SELECT (CASE WHEN effective_date = prior_effective_date
THEN null
ELSE effective_date
END) effective_date,
(CASE WHEN end_date = prior_end_date
THEN null
ELSE end_date
END) end_date,
description ,
subpart
FROM
(SELECT effective_date,
lag(effective_date) over
(order by effective_date desc, description asc) prior_effective_date,
end_date,
lag(end_date) over
(order by effective_date desc, description asc) prior_end_date,
desc,
subpart
FROM <<your query>>)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2614 次 |
| 最近记录: |