如何对货币ID配对表去重?SQL语句优化求助
货币配对去重SQL解决方案
修改后的SQL语句如下:
with cte_currencies as ( select 'GBP' as CurrencyId union select 'USD' union select 'DKK' union select 'GBX' ), cte_combined_crossings as ( select b.CurrencyId as baseCurrencyId, q.CurrencyId as QuoteCurrencyId from cte_currencies b cross join cte_currencies q where b.CurrencyId < q.CurrencyId ) select * from cte_combined_crossings
实现思路
把原SQL的过滤条件从not b.CurrencyId=q.CurrencyId替换成b.CurrencyId < q.CurrencyId,通过字典序比较货币ID,只保留基准货币ID字典序小于报价货币ID的配对,自动剔除DKK/GBP与GBP/DKK这类双向重复的条目,输出结果完全匹配目标表需求。
内容的提问来源于stack exchange,提问作者Liam
相关产品推荐
相关产品推荐

