我正在寻找一些帮助与以下任务。我正在从Map表MAP_RECRF_ASSET_TYPE中存在ASSET_TYPE的MY_TABLE中选择记录,但我得到了重复的TECHNICAL_ID。
为此,我在MAP_RECRF_ASSET_TYPE中添加了PRIORITY列,并且在结果中,我希望每个TECHNICAL_ID只有一条记录,该TECHNICAL_ID具有MAP_RECRF_ASSET_TYPE表中的ASSET_TYPE,但也具有最低的PRIORITY值(对于相同的PRIORITY值,始终只有1条记录)。
我的数据库是Oracle 19 c。
非常感谢你帮助我写这段代码。谢谢
我的当前查询:
with MY_TABLE as (
select '1111' as TECHNICAL_ID, 'NOTIONALCR' as ASSET_TYPE from dual union all
select '1111' as TECHNICAL_ID, '50000' as ASSET_TYPE from dual union all
select '2222' as TECHNICAL_ID, 'FWDNOTLCR' as ASSET_TYPE from dual union all
select '2222' as TECHNICAL_ID, '50000' as ASSET_TYPE from dual union all
select '3333' as TECHNICAL_ID, '50000' as ASSET_TYPE from dual union all
select '3333' as TECHNICAL_ID, 'DUMMY' as ASSET_TYPE from dual
),
MAP_RECRF_ASSET_TYPE as (
select 'SW' as APPLICATION, 'NOTIONALCR' as ASSET_TYPE, 1 as PRIORITY from dual union all
select 'SW' as APPLICATION, 'NOTIONALDB' as ASSET_TYPE, 1 as PRIORITY from dual union all
select 'SW' as APPLICATION, 'FWDNOTLCR' as ASSET_TYPE, 2 as PRIORITY from dual union all
select 'SW' as APPLICATION, 'FWDNOTLDR' as ASSET_TYPE, 2 as PRIORITY from dual union all
select 'SW' as APPLICATION, 'SWOFFBALCR' as ASSET_TYPE, 2 as PRIORITY from dual union all
select 'SW' as APPLICATION, 'SWOFFBALDR' as ASSET_TYPE, 2 as PRIORITY from dual union all
select 'SW' as APPLICATION, 'SWFWNOTLCR' as ASSET_TYPE, 2 as PRIORITY from dual union all
select 'SW' as APPLICATION, 'SWFWNOTLDB' as ASSET_TYPE, 2 as PRIORITY from dual union all
select 'SW' as APPLICATION, '50000' as ASSET_TYPE, 3 as PRIORITY from dual
)
SELECT *
FROM MY_TABLE x
WHERE
x.ASSET_TYPE IN (
SELECT ASSET_TYPE
FROM MAP_RECRF_ASSET_TYPE
WHERE APPLICATION = 'SW');
--目前的成果:
TECHNICAL_ID ASSET_TYPE
1111 NOTIONALCR
1111 50000
2222 FWDNOTLCR
2222 50000
3333 50000
--预期成果:
TECHNICAL_ID ASSET_TYPE
1111 NOTIONALCR
2222 FWDNOTLCR
3333 50000
2条答案
按热度按时间f87krz0w1#
您可以在连接两个表的位置添加另一个CTe,它将选择其中一个优先级并添加按优先级排序的行号
| 技术参数|资产类型|
| --|--|
| 1111 |概念|
| 2222 |FWDNOTLCR|
| 3333 | 50000 |
fiddle
ax6ht2ek2#
您可以使用
INNER JOIN
,然后使用ROW_NUMBER
解析函数来查找每个technical_id
的最低优先级:其输出:
| 技术参数|资产类型|
| --|--|
| 1111 |概念|
| 2222 |FWDNOTLCR|
| 3333 | 50000 |
fiddle