请参阅下表以供参考。我需要返回包含最小值的重复行。
在这个例子中,我想只显示SLAT_LEN = 30或者最小SLAT_LEN的2行。我试过排名,但是当我这样做的时候连续排名。我希望重复的大小具有相同的排名并按大小连续排名。
尺寸发生变化,所以我不能只使用像SLAT_LEN = 30这样的条件。
或者我应采取不同的方法吗?
select *
from(
select lg.wd_demand_id,wm.slat_len, wm.prof_size, wm.wd_material_id, wm.color, rank() over ( partition by wm.slat_len order by lg.wd_demand_id) as rank
from wd_demand_log lg, wd_bins wb, wd_material wm, wd_bins_material wbm
where lg.wd_bins_id = wb.wd_bins_id
and lg.wd_material_id = wm.wd_material_id
and lg.wd_bins_id = wbm.wd_bins_id
and lg.wd_material_id = wbm.wd_material_id
AND lg.plant_id = 44 AND lg.dept_id = 220 AND wb.plant_id = 44 AND wb.dept_id = 220
AND NOT EXISTS(SELECT dmpln.wd_demand_id FROM wd_demnd_pln_inv dmpln WHERE dmpln.wd_demand_id = lg.wd_demand_id)
AND wm.prof_size = '2' AND wm.color = 450
AND lg.wd_po_error is null)
答案 0 :(得分:2)
您可以使用PARTITION BY
来指定排名范围。 ORDER BY
指定排名。所以在你的情况下,你需要像
rank() over (order by wm.slat_len)
如果您使用的是Oracle 12c,则可以使用FETCH FIRST
限制结果:
ORDER BY wm.slat_len
FETCH FIRST 1 ROW WITH TIES;
答案 1 :(得分:0)
也许我没有正确理解问题,但这是我的潜在解决方案:
WITH aset
AS (SELECT lg.wd_demand_id
, wm.slat_len
, wm.prof_size
, wm.wd_material_id
, wm.color
FROM wd_demand_log lg, wd_bins wb, wd_material wm
, wd_bins_material wbm
WHERE lg.wd_bins_id = wb.wd_bins_id
AND lg.wd_material_id = wm.wd_material_id
AND lg.wd_bins_id = wbm.wd_bins_id
AND lg.wd_material_id = wbm.wd_material_id
AND lg.plant_id = 44
AND lg.dept_id = 220
AND wb.plant_id = 44
AND wb.dept_id = 220
AND NOT EXISTS
(SELECT dmpln.wd_demand_id
FROM wd_demnd_pln_inv dmpln
WHERE dmpln.wd_demand_id = lg.wd_demand_id)
AND wm.prof_size = '2'
AND wm.color = 450
AND lg.wd_po_error IS NULL)
SELECT *
FROM aset
WHERE slat_len = (SELECT MIN (slat_len)
FROM aset)