我有这个查询(Oracle 11g):
SELECT
distinct to_number(val.number_value), val.variant_number
from
val
left join
mad on mad.id = val.madid
where
val.id = 227753377 and mad.fk_parent =18468
and (val.language_id = 6003 or val.language_id is null)
and mad.identifier = 'IDENTIFIER2'
order by val.variant_number
返回给我:
0 | 1
0 | 2
0 | 3
0 | 4
0 | 5
在这种情况下,Oracle不知道该怎么做,因为它引用了所有选定的列,我想。
上面这种情况我需要的是这个结果:
0 | 1
但在另一个例子中它应该给我:
0 | 1
1 | 2
2 | 3
如果val.number_value是重复的,那么它应该被忽略并为此仅返回1行。 怎么做?
答案 0 :(得分:3)
如果每个number_value
只需要一个variant_number
,则可以使用group by
和一些汇总功能(例如最小/最大):
SELECT
to_number(val.number_value), min(val.variant_number)
from
val
left join
mad on mad.id = val.madid
where
val.id = 227753377 and mad.fk_parent =18468
and (val.language_id = 6003 or val.language_id is null)
and mad.identifier = 'IDENTIFIER2'
group by to_number(val.number_value)
答案 1 :(得分:2)
with numbers as (
SELECT to_number(val.number_value) as number_value,
val.variant_number,
row_number() over (partition by val.number_value order by val.variant_number) as rn
from val
left join mad on mad.id = val.madid
where val.id = 227753377 and mad.fk_parent =18468
and (val.language_id = 6003 or val.language_id is null)
and mad.identifier = 'IDENTIFIER2'
order by val.variant_number
)
select number_value,
variant_Number
from numbers
where rn = 1;
通过更改窗口功能中的order by
,您可以“配置”从重复项中选择哪一行。