I have a query that returns something like this...
Dataset 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
I'm looking to get a query that returns that dataset like this...
Dataset 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
Notice that the duplicate effective date( 4/10/2011 - {null} and 4/1/2011 - 4/9/2011) are suppressed when duplicated.
Edit 1: In response to @Justin Cave's answer,
Below is my query merged with Justin Cave's template. It's close, but its a little off. The dates and descriptions seem a little mixed up from what they're supposed to be (the data in the dataset should be like the one in Dataset 2:. I think it may have something to do with my ordering but I'm not sure.
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
);
Edit 2: The incorrect data was due to ambiguously defining the column effective date. The query above now works correctly.
I think you're looking for something like this using analytic functions. It's not clear to me how the result set is ordered, so you'll have to fill that in. You'll also probably be able to combine the analytic function into your query rather than adding a third level of nesting.
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>>)
If you're using SQL*Plus to generate the output, you can use BREAK
:
BREAK ON EFFECTIVE_DATE ON END_DATE
SELECT ...
精彩评论