@param
我通过找到重复项来获得此结果,但是在这里我想根据列的结果删除它。
我的首要任务是保留记录为" IsActive " = 1并删除其他记录,无论是1还是0.
但如果没有记录" IsActive " = 1然后优先级保持任何一个0。 因此,将有两个记录 - 第1行和第5行。
感谢您的帮助!谢谢!
答案 0 :(得分:1)
使用cte
删除重复项并使用row_number
功能
insert @table (id,orgname,TaxID,IsActive)
select 1 , 'Miller' , 111 , 1 union all
select 1 , 'Miller' , 111 , 1 union all
select 1 , 'Miller' , 111 , 0 union all
select 1 , 'Miller' , 111 , 0 union all
select 2 , 'XYZ' , 222 , 0 union all
select 2 , 'XYZ' , 222 , 0
;
with cte as (
SELECT *, ROW_NUMBER() over (partition by id order by IsActive desc) rn from @table)
delete from cte where rn>1
select * from @table
答案 1 :(得分:0)
对于您的样本数据,这可以满足您的需求:
select id, orgname, taxid, max(isactive) as isactive
from t
group by id, orgname, taxid;