从 DB2 中的联合查询中删除数据
Dedup from union Query in DB2
在 union 中,我有来自两个系统的记录,这些记录可能相似,我不关心来自哪个系统 table 但我只需要保留一个并删除另一个。
这是 table 的样子:
Select Agent, ID, System Name, APPID, Sum(Count) From Table 1
Union All Select Agent, ID, System Name, APPID, Sum(Count) From Table 2
UNION 而不是 UNION ALL 可以完成这项工作
作为替代方案,您也可以使用
with base as (
Select Agent, ID, System_Name, APPID, Sum(Count) as count From Table 1
Union All Select Agent, ID, System_Name, APPID, Sum(Count) as count From Table 2
)
, temp as (
select Agent, ID, System Name, APPID, count
row_number() over (partition by Agent, ID, APPID) as rownum
from base
)
select Agent, ID, System Name, APPID, count
from temp
where rownum = 1
由于目前缺少 iSeries,我还没有对其进行测试,但至少我希望这个想法应该很清楚
补充:如果您需要有特定的排序偏好,您还可以在 PARTITON BY 之后添加一个 ORDER BY
在 union 中,我有来自两个系统的记录,这些记录可能相似,我不关心来自哪个系统 table 但我只需要保留一个并删除另一个。
这是 table 的样子:
Select Agent, ID, System Name, APPID, Sum(Count) From Table 1
Union All Select Agent, ID, System Name, APPID, Sum(Count) From Table 2
UNION 而不是 UNION ALL 可以完成这项工作
作为替代方案,您也可以使用
with base as (
Select Agent, ID, System_Name, APPID, Sum(Count) as count From Table 1
Union All Select Agent, ID, System_Name, APPID, Sum(Count) as count From Table 2
)
, temp as (
select Agent, ID, System Name, APPID, count
row_number() over (partition by Agent, ID, APPID) as rownum
from base
)
select Agent, ID, System Name, APPID, count
from temp
where rownum = 1
由于目前缺少 iSeries,我还没有对其进行测试,但至少我希望这个想法应该很清楚
补充:如果您需要有特定的排序偏好,您还可以在 PARTITON BY 之后添加一个 ORDER BY